VS 2010 ASP.NET 4.0 RedirectPermanent does a 301 and not 302

Hi,

As I was reading a white-paper on changes in VS 2010, ASP.NET 4.0, I came across this new method that the Response object exposes – ‘RedirectPermanent’. This issues a ‘HTTP 301 – moved permanently’ as against a ‘HTTP 302 – moved temporarily’ that was issued by the old Redirect method both in traditional and MVC – yes I know, in MVC there is a way to create our own custom action result that does a HTTP 301 redirect, but that’s still some work.

Testing this was simple; Created a new asp.net web application, added button on the default.aspx page:

   1:  <asp:Button ID="Redirect" runat="server" Text="Redirect using 301" onclick="Redirect_Click" />

In the code behind, called RedirectPermanent:

   1:  protected void Redirect_Click(object sender, EventArgs e)
   2:  {
   3:      Response.RedirectPermanent("~/About.aspx");
   4:  }

Now, to test if this was actually doing a 301, I used our good-old friend – Fiddler. So, fired up the application and since Fiddler has some issues with using ‘localhost’, I changed it to http://127.0.0.1.:49319/ (note the extra . in the end). Sure enough, saw the calls to my application in Fiddler.

Clicked on the button, the About.aspx page was loaded and now for the moment of truth <drumroll/>, YES IT’S A 301 and Fiddler confirms it:

redirectUsing301

Why is this useful? Basically, search engines store the new URL associated with permanent redirects and this eliminates an futile round-trip made by the browser for temp (302) redirects.

3 Comments

  • Actually, I forgot to write this.
    Fiddler does show a 302 when you do a Response.Redirect from a ASP.NET 3.5 application.
    My apologies, I wanted to include that in the main article itself.

  • hi,Thank for your post! Excellent!!But Could you put it to download in High Definition, buescae it can't be seen so clearly in order to understand or if you send me the souce code too.Regards,Angel Santomas

  • Angel,

    Not sure what you want me to send in hi-def, but as far as the source code is concerned, just follow the blog and you can get the source code in a matter of minutes.

    Arun

Comments have been disabled for this content.