asp.netiishttp-redirect

ASP.NET - redirect 301


How do I redirect permanently in ASP DOT NET? I'd like to do a 301 redirect from one page on my site to another page.


Solution

  • protected void Page_PreInit(object sender, EventArgs e)
    {
        Response.StatusCode = 301;
        Response.StatusDescription = "Moved Permanently";
        Response.RedirectLocation = "AnotherPage.aspx";
        HttpContext.Current.ApplicationInstance.CompleteRequest();
    }
    

    And in 4.0, there's a simple HttpResponse.RedirectPermanent() method that does everything above for you:

    Response.RedirectPermanent("AnotherPage.aspx");