8

I have a bigger project with about 9 controllers. Now at the end of the project the url requeirement change. How to deal best with this situation - renaming the controllers seems just too cumbersome ... I need to change all links in servercode and javascript

Marc Loeb
  • 570
  • 9
  • 27

2 Answers2

6

Your problem can be solved by changing your existing routes. In your global.asax you'll find a code fragment like this

routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );

That maps an URL '/Controller/Action/Id' to Controller, Action and Id. You can provide routes like this

    routes.MapRoute(
            "RefactoredRoute", // Route name
            "SomeChangedURLBase/{action}/{id}", // URL with parameters
            new { controller = "Controller", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );

to route requests to /SomeChangedURLBase... to be handled by Controller.

Be aware that these routes should be registered before the default route to avoid that links generated in views point to the default route and generate the old URL.

saintedlama
  • 6,838
  • 1
  • 28
  • 46
  • one point I realized after implementing this: Inside my browser I still see the old urls if I browse regularily the application, but my new urls are accepted. Any way of changing this - so that the new urls are shown? – Marc Loeb Apr 26 '12 at 14:49
  • This may be a problem with Route ordering/ActionLink building. You could use RouteLink instead of ActionLinks to generate the links. – saintedlama Apr 26 '12 at 14:57
  • If you are using ActionLinks on your pages, and point them to the controller and action, it will use the new URL if it is placed before the old one in the routes collection. – Robert McKee Nov 19 '14 at 15:45
1

you could change the routings in global.asax

just change the method RegisterRoutes

here you can find some more informations.

http://www.asp.net/mvc/tutorials/older-versions/controllers-and-routing/asp-net-mvc-routing-overview-cs

http://weblogs.asp.net/scottgu/archive/2007/12/03/asp-net-mvc-framework-part-2-url-routing.aspx

cheers

nWorx
  • 2,145
  • 16
  • 37