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: ,


7 Comments

  • 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.

  • 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

  • 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();

  • 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)??

  • 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

  • in asp.net 3.5
    i do 301 redirect page on page load
    HttpContext context = this.Context;
    context.Response.Clear();
    context.Response.Status = "301 Moved Permanently";
    context.Response.AddHeader("Location", url);
    context.Response.StatusCode = 301;
    context.Response.End();

    instead of End(), if u use CompleteRequest(), the following code after CompleteReqeust() execute and also other method like page-Prerender() method.

    since we just need to redirect wtihout doing any processing is there any way that stop rest execution like End() and also dont throw the error?

  • RedirectPermanent is simpler, but it doesn't roll off the tongue.. I often think it is PermanentRedirect...

    Other times I find it reassuring to still use AddHeader and Status.

    Oh, the options...

Comments have been disabled for this content.