How can I have lowercase, plus underscore if possible, routes in ASP.NET MVC? So that I would have /dinners/details/2
call DinnersController.Details(2)
and, if possible, /dinners/more_details/2
call DinnersController.MoreDetails(2)
?
All this while still using patterns like {controller}/{action}/{id}
.
These two tutorials helped when I wanted to do the same thing and work well:
http://www.coderjournal.com/2008/03/force-mvc-route-url-lowercase/ http://goneale.com/2008/12/19/lowercase-route-urls-in-aspnet-mvc/
EDIT: For projects with areas, you need to modify the GetVirtualPath() method:
I found this at Nick Berardi’s Coder Journal, but it did not have information on how to implement the
LowercaseRoute
class. Hence reposting here with additional information.First extend the
Route
class toLowercaseRoute
Then modify the
RegisterRoutes
method of Global.asax.csI would however like to know a way to use routes.MapRoute...
You can continue use the MapRoute syntax by adding this class as an extension to RouteCollection:
Now you can use in your application's startup "MapRouteLowerCase" instead of "MapRoute":
If you are using the UrlHelper to generate the link, you can simply specify the name of the action and controller as lowercase:
Results in: /media/photos/delete/64 (even though my controller and action are pascal case).
With System.Web.Routing 4.5 you may implement this straightforward by setting LowercaseUrls property of RouteCollection:
Also assuming you are doing this for SEO reasons you want to redirect incoming urls to lowercase (as said in many of the links off this article).
Could you use the ActionName attribute?
I don't think case matters. More_Details, more_DETAILS, mOrE_DeTaILs in the URL all take you to the same Controller Action.