1

I am converting an old ASP.NET web forms site to ASP.NET MVC 5. I would like to issue permanent redirects for the old page URLs.

Here is what I have done -

RouteConfig.cs:

routes.MapRoute("About_old", 
      "About/About.aspx", 
      new { controller = "Home", action = "About_old" });

HomeController.cs:

public ActionResult About_old()
{
   return new RedirectResult("/About", true);

   // I've also tried 
   // return RedirectToActionPermanent("About"); 
   // return RedirectPermanent("/About");
}

All attempts load the correct /About view, however the URL does not change, and I do not see a 301 code in the response. In other words, the URL is "localhost/About/About.aspx" and I expect it to be "localhost/About"

Complete Request/Repsonse from Chrome:

Request URL:http://localhost:55774/About/About.aspx
Request Method:GET
Status Code:200 OK

Request Headers
GET /About/About.aspx HTTP/1.1
Host: localhost:55774
Connection: keep-alive
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.117 Safari/537.36
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8

Response Headers
Cache-Control:private
Content-Encoding:gzip
Content-Length:2284
Content-Type:text/html; charset=utf-8
Date:Sat, 01 Mar 2014 18:10:41 GMT
Server:Microsoft-IIS/8.0
Vary:Accept-Encoding
X-AspNet-Version:4.0.30319
X-AspNetMvc-Version:5.1
X-Powered-By:ASP.NET

Nowhere do I see a 301 and the URL does not change. I have to think this has to do with how I am mapping the route of the old aspx page in RouteConfig.cs as all action methods have the same results. NOTE I have put a solution using global.asax below, however I would prefer it to work as I am attempting above, so I have not accepted my answer.

Am I doing something wrong or just missing something? How do I get the 301 to issue and URL to change?

tereško
  • 58,060
  • 25
  • 98
  • 150
user210757
  • 6,996
  • 17
  • 66
  • 115
  • I would think your question should be "why didn't RedirectPermanent send a 301" [as specified in the MSDN](http://msdn.microsoft.com/en-us/library/dd384856(v=vs.110).aspx)... and not copy code from 2010, 2 months before RedirectPermanent even existed. While valid, there should be no need for the workaround you provide as an answer. Now you are executing that all the time, instead of on the page you needed. – MikeSmithDev Mar 01 '14 at 19:36
  • RedirectPermanent does not change the URL. That is what I need to do. If you can show me how to do this with RedirectPermanent, I will accept your answer. – user210757 Mar 01 '14 at 19:57
  • MikeSmithDev - updated question per your suggestions – user210757 Mar 01 '14 at 20:18

1 Answers1

3

Here is my solution (Global.asax)

protected void Application_PreRequestHandlerExecute(Object sender, EventArgs e)
        {
            string currentUrl = HttpContext.Current.Request.Path.ToLower();
            if (currentUrl.EndsWith("/about/about.aspx"))
            {
                Response.Status = "301 Moved Permanently";
                Response.AddHeader("Location", "/About");
                Response.End();
            }
        }

From answer here: Global 301 redirection from domain to www.domain

Community
  • 1
  • 1
user210757
  • 6,996
  • 17
  • 66
  • 115