.NET Articles

ASP.NET MVC routing to handle legacy URLs passing query strings


Assume web form application is migrated to MVC application. Legacy web form url is http://localhost/Emp/EmpDetails.aspx?id=100 which was using for display employee details. New MVC url for the same page is http://localhost/Emp/Employee/Details/100 (Controller=Employee, Action=Details, parameter=100).

Search engine crawlers update their search database with new urls corresponding to legacy urls only if legacy URLs return Response Status as "301 Moved Permanently" with new url location. This procedure helps website to maintain the same page rankings as before in search engine.

Follow below code changes to handle legacy URLs.

Add Route in Global.asax as shown below

    routes.MapRoute(
                "legacyUrl1",
                "{controller}.aspx/{action}/{id}",
                new { controller = "Articles", action = "RedirectLegacyURL", id = UrlParameter.Optional } 
            );
Create a method as shown below in controller class to redirect legacy url to equivalent new url.
   public void RedirectLegacyURL(string id)
        {
            Response.Status = "301 Moved Permanently";
            Response.AddHeader("Location", "Articles/article/" + id);
            Response.End();
        }

    

Example of different types of valid route template in Global.asax

Map Route Template URL Example Descripiton
{controller}.aspx/{action}/{id} ~/Articles.aspx?id=100 Controller=Articles, id=100(Action=this can not be taken from URL, default action  will be considered from the MapRoute() method).
Archive/{controller}.aspx/{action}/{id} ~Archive/Articles.aspx?id=100 Controller=Articles, id=100(Action=this can not be taken from URL, default action  will be considered from the MapRoute() method).
{controller}.aspx/{action}/{id} ~/Articles.aspx/Article/100 Controller=Articles,Action=Article,id=100
{controller}.aspx-{action}/{id} ~/Articles.aspx-Article/100 Controller=Articles,Action=Article,id=100
{controller}.aspx-{action}-{id} ~/Articles.aspx-Article-100 Controller=Articles,Article=Article,id=100

Example of incorrect route mappings in Global.asax

Map Route Template URL Example Description
{controller}.aspx/{action}/{id} ~/Articles.aspx?Article/id=100 INCORRECT url. If "?" is used in url then remove the Action part. Correct url is ~/Articles.aspx?id=100. In this case Action will be taken from default Action declared in routes.MapRoute() method.
Archive{controller}.aspx/{action}/{id} ~/ArchiveArticles.aspx/Article/100 INCORRECT url. Use"{" or "-" in URL mapping template for seperation.
{controller}.aspx-{action}-{id} ~/Articles.aspx-Article-100-ID INCORRECT url. Route syntax is correct but URL has more "-" symbols than url template. URL works fine if "-ID" removed from URL.

We can follow other techniques as well to handle legacy urls. Refer following links for the same.

http://www.mikesdotnetting.com/Article/108/Handling-Legacy-URLs-with-ASP.NET-MVC

http://www.asquestion.com/question/29406245378435437/ASP-Net-MVC-routing-legacy-URLs-passing-querystring-Ids-to-controller-actions