Search code examples
c#asp.netresponseascx

Response.AddHeader in an ascx file


Is it possible to use the Response.AddHeader particularly Im trying to use the "Refresh" of it. I need it to pause before it redirects but the place where the code is being ran is in a ASCX in the codebehind. It does nothing when I have the following in my Codebehind:

HttpContext.Current.Response.AddHeader("Refresh", "6;URL=Default.aspx");

Any way I can redirect a user in the codebehind on a ascx page?


Solution

  • I don't know why that isn't working for you - it seems to be the right syntax to add the redirect to your HTTP header, and it works for me on several browsers. To debug this, you can run Fiddler or a similar tool to see the full HTTP response, and see if it's not making it to the header.

    You may want to try reformatting slightly - the examples I've seen have a space between the semi-colon and the url, and url keyword in lower-case. I doubt this is the problem, but worth a try, if it's failing on specific browsers:

    Refresh: 0; url=http://www.example.com/
    

    An alternative approach, maybe easier to debug, is to use a meta tag instead of HTTP header, so it actually shows up in the markup. Something like:

    var metaControl = new HtmlMeta
      {
        Content = "2;url=http://webdesign.about.com/",
        HttpEquiv = "refresh"
      };
    Page.Header.Controls.Add(metaControl);
    

    This will add a <meta> tag to your <head> section, which should have the desired effect.