views:

856

answers:

4

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.

A: 

Response.Redirect

EDIT: My bad, I misread the question. Blame the time :)

Jan Jungnickel
+3  A: 

Have a look here

Response.Redirect will give you a 302 rather than a 301.

Matthew Steeples
+20  A: 
protected void Page_PreInit(object sender, EventArgs e)
{
    Response.StatusCode = 301;
    Response.StatusDescription = "Moved Permanently";
    Response.AddHeader("Location", "AnotherPage.aspx");
}
cxfx
In my helper class, I've created an extension method that allows me to type: Response.PermananentRedirect("someUrl.aspx"); - method call is: public static void PermanentRedirect(this System.Web.HttpResponse response, string uri)
Zhaph - Ben Duguid
Nice. Can set Response.RedirectLocation instead of calling Response.AddHeader.
orip
+5  A: 

ASP.NET 4.0 Beta 1 has a Response.RedirectPermanent() method for doing 301 redirects, e.g.

Response.RedirectPermanent("AnotherPage.aspx");

From the ASP.NET 4.0 and Visual Studio 2010 Web Development Beta 1 Overview white paper:

It is common practice in Web applications to move pages and other content around over time, which can lead to an accumulation of stale links in search engines. In ASP.NET, developers have traditionally handled requests to old URLs by using by using the Response.Redirect method to forward a request to the new URL. However, the Redirect method issues an HTTP 302 Found (temporary redirect) response, which results in an extra HTTP round trip when users attempt to access the old URLs.

ASP.NET 4.0 adds a new RedirectPermanent helper method that makes it easy to issue HTTP 301 Moved Permanently responses.

cxfx
+1 This is becoming more correct as .Net 4 rolls out.
Andrew M