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