Permanent Redirect in Asp.net 4.0

 

Hi,

 

One of the most important things for web 2.0 sites are to be searchable, because every one wants top get more and more users for the search result.

For search engine Optimisation, every small aspect of the page is Important. One of the important things to do for this is to make sure all permanent redirection in your site is made with the 301 Host headers. I had earlier posted about this here.

 

With the new features in Dot net framework 3.5 we could have easily made this much simpler by adding an extension methods to the response class and make it look simple.

public void PermanentRedirect(this HttpResponse response, string destination)

{

        response.Status = "301 Moved permanently";

        response.AddHeader("Location", destination);

        response.End();

}

 

In the above code we have added a simple Extension method to the HttpResponse class. What this method does is always makes the redirect to a new page Permanent by adding appropriate response header.

 

Hence after this extension method we can easily makes call like Response.PermanentRedirect(“http://www.vikramlakhotia.com”);

After looking at the importance of this functionality Microsoft has decided to add this functionality in the Asp.net framework itself. Now with Asp.net 4.0 new RedirectPermanent helper method that makes it easy to issue HTTP 301 moved permanently responses

 

Vikram

1 Comment

  • Consider adding a response.Clear() to the begining of your extension method to clear the response stream before writing your header.

Comments have been disabled for this content.