Response.RedirectPermanent

In ASP.NET 4.0, there is a new Method called RedirectPermanent() on the HttpResponse Object. This method acts similar to Response.Redirect() except that the HTTP Response Code it sends out is 301 which indicates that the Page or Resource has moved permanently. Response.Redirect() returns Response Code 302, which indicates a temporary redirect.

While to the user the effect of both methods are exactly same, to search engines they are not.

If you use the Response.Redirect() to redirect from Page A to Page B, search engines will keep Page A in their index since the Response.Redirect() indicates a temporary redirect. In case the original page (Page A) no longer exists, and Page B should replace Page A, you need to indicate it with a Response Code 301, thus the Response.RedirectPermanent() method.

It took them about 10 years, but I am glad that they have finally decided to add it to ASP.NET

Meanwhile, we have had our own implementation of this method for a long time, and with the help of an Extension Method, you do not have to wait for ASP.NET 4.0 to use this method.

Here is Response.RedirectPermanent:

/* Author: Abu Haider */
/* Aug 2009 */
/* Adds an Extension Method to HttpResponse Class: Response.RedirectPermanent */
/* www.haiders.net */

public static class Extensions
{
    public static void RedirectPermanent(this HttpResponse Response, string PathOrUrl)
    {
        Response.Clear();
        Response.Status = "301 Moved Permanently";
        Response.RedirectLocation = PathOrUrl;
        Response.End();   
    }
}

Drop this class in your App_Code folder and you will have the Response.RedirectPermanent() method available on HttpResponse Object. Extension method only works in ASP.NET 3.5

When you upgrade to ASP.NET 4.0, the built in instance method in the Framework will take precedence and this method will no longer be used by the compiler.

Use RedirectPermanent() where it is appropriate. This will help search engines keep a more accurate index of your Websites.

Posted on August 12, 2009 04:17 by Haider

Comments

May 8. 2010 06:25

bitlink

Thanks for the code. By the way - If you don't want to put it in an App_Code folder(I don't have one in my webapp project) then you can simply put it any class and leave the namespace empty. Can still access it as Response.RedirectPermanent(...)
thank you very much!

bitlink

Don't Post SPAM

If you are posting a commment just to get a link, don't waste your time!

I have a sophisticated comment moderation system in place, and your comment will not be posted.

Add comment




biuquote
  • Comment
  • Preview
Loading