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
Asked
Active
Viewed 6,579 times
8
-
You can also look at IIS Url Rewrite – Chandermani Apr 24 '12 at 14:28
-
ReSharper6 is the one that does job.. – Alexander Beletsky Apr 24 '12 at 15:48
-
Also note: you can use dashes in actions that need to be specified in the url, in defiance of C# method naming rules (it took me a while to learn that). see: http://stackoverflow.com/questions/30310/asp-net-mvc-how-do-i-enable-dashes-in-my-urls – Faust Apr 24 '12 at 16:50
2 Answers
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://weblogs.asp.net/scottgu/archive/2007/12/03/asp-net-mvc-framework-part-2-url-routing.aspx
cheers

nWorx
- 2,145
- 16
- 37