Search code examples
c#asp.netresponse.redirectmaster-pages

Masterpage to detect response redirect


I want to clear a placeholder control on my masterpage every time a Redirect is being made. How can I achieve that in codebehind?

I could check whether the last saved url and the current url match, but that is a really a makeshift solution I don't wanna' go for.

Something like [if(//Page Redirect detected){//do something}


Solution

  • I would simply dump a flag into session when you do a redirect. Check the flag on each load in the master page and clear it so subsequent requests do not unnecessarily detect it. Perhaps you can create a redirection helper class to centralize the flag-setting responsibility.

    if (Session["RedirectFlag"] != null && (bool)Session["RedirectFlag"])
    {
        // clear your placeholder
        Session.Remove("RedirectFlag"); // clear the flag
    }
    

    ..

    public static class HttpResponseExtension
    {
        public static void RedirectWithFlag(this HttpResponse response, string url)
        {
            response.RedirectWithFlag(url, true);   
        }
    
        public static void RedirectWithFlag(this HttpResponse response, string url, bool endResponse)
        {
            System.Web.HttpContext.Current.Session["RedirectFlag"] = true;
            response.Redirect(url, endResponse);
        }
    }