How to 301 Permanent Redirect in ASP.NET

In the next release of the .NET Framework (.NET Framework 4.0) there is a new response method for permanently redirecting a request: Response.RedirectPermanent.  You can see the Beta MSDN documentation for Response.RedirectPermanent here.  This will automatically issue the 301 moved permanently status code and redirect to the target page.  A permanent redirect status code tells a search engine to update their cache and reassign the old url to the new url.

But if you need to do this now (prior to .NET Framework 4.0) you will need to do it the manual way.  To do so you will need to manually add the status and location headers to the response. 

The pre .NET Framework 4.0 versions are:

  Response.Status = "301 Moved Permanently";
  Response.AddHeader("Location", "/");

  Response.Status = "301 Moved Permanently";
  Response.AddHeader("Location", "/");
  Response.End();

There is also a RedirectToRoutePermanent method which will allow you to redirect to a new url using route parameters (and sends the 301 Moved Permanently status code). 

UPDATE: See RichardD's comment below for a different method.  I have not tried it that way but I will next time because it certainly does look much cleaner.

Technorati Tags: ,


Published 31 January 2010 06:32 AM by Jeff Widmer
Filed under: , ,

Comments

# RichardD said on 01 February, 2010 03:20 PM

Another alternative that doesn't rely on hard-coded strings:

Response.Redirect(newLocation, false);

Response.StatusCode = 301;

Response.End();

This will correctly expand the path, apply the app-path modifier if necessary, and URL-encode the new location.

It will also throw an exception if you use it from a call-back, correctly handle smart-navigation post-backs, and output the recommended "Object moved" HTML document in case the browser doesn't follow the redirection.

# Twitter Trackbacks for How to 301 Permanent Redirect in ASP.NET - Jeff Widmer's Blog [asp.net] on Topsy.com said on 02 February, 2010 12:59 AM

Pingback from  Twitter Trackbacks for                 How to 301 Permanent Redirect in ASP.NET - Jeff Widmer's Blog         [asp.net]        on Topsy.com

# webbes said on 03 February, 2010 11:17 AM

Another even aproach would be this one:

public static class HttpResponseUtility {

  public static void RedirectPermanent (

     this HttpResponse response,

     string url) {

         response.RedirectPermanent(url, true);

  }

  public static void RedirectPermanent (

     this HttpResponse response,

     string url,

     bool endResponse) {

        response.Redirect(url, false);

        response.StatusCode = 301;

        if(endResponse){

           response.End();

       }

   }

}

And there we've extended the HttpResponse in Asp.Net 3.5 with the new 4.0 methods.

Cheers,

Wesley

# MicroTrends said on 26 October, 2010 05:00 AM

Response.StatusCode = 301;

Response.Status = "301 Moved Permanently";

Response.RedirectLocation = "http://www.newurl.com ";

Response.End();

# Arni said on 06 July, 2011 07:14 PM

Even a cleaner way if you don't want to hardcode the number:

Response.Redirect(newLocation, false);

Response.StatusCode = (int)System.Net.HttpStatusCode.MovedPermanently;

Response.End();

# Kevin said on 27 December, 2011 07:33 PM

PERFORMANCE WARNING:

See "Remarks" in MSDN docs:

msdn.microsoft.com/.../t9dwyts4.aspx

Calling Redirect(String) is equivalent to calling Redirect(String, Boolean) with the second parameter set to true.

Redirect calls End() which throws a ThreadAbortException exception upon completion. This exception has a detrimental effect on Web application performance. Therefore, we recommend that instead of this overload you use the HttpResponse.Redirect(String, Boolean) overload and pass false for the endResponse parameter, and then call the CompleteRequest() method. For more information, see the End method.

----------

Therefore, for pre- .NET 4.0 try:

Response.Redirect(newLocation, false);

Response.StatusCode = (int)System.Net.HttpStatusCode.MovedPermanently;

HttpApplication.CompleteRequest();

# Brij Sharma said on 03 January, 2012 01:26 PM

Could you please tell how to redirect when routing is used in the website and in case where there are large number of pages which have to be redirected (old urls are stored in database)??

# Jeff Widmer said on 04 January, 2012 09:25 AM

Hi Brij Sharma,

Take a look at UrlRewrite (IIS7 component) to handle routing and redirecting when there are a large number of redirects necessary.

-Jeff

Leave a Comment

(required) 
(required) 
(optional)
(required) 

Search

Go

This Blog

News

Syndication