ASP.NET MVC 2 controllers and actions use UpperCamelCase.
For some reasons many big sites, including SO, use lowercase (with underscore) for controllers and actions in the urls. Examples:
http://stackoverflow.com/questions
http://stackoverflow.com/users/377920/randomguy
http://www.reddit.com/ad_inq/
http://www.wired.com/special_multimedia/mobile/
etc.
I would like to know how this is accomplished.
The default router seems to be case-insensitive, ie. stackoverflow.com/questions/ask
will be directed to Questions-controller's Ask() method without a problem.
However, say we want to direct questions/add_to_favorites
to Questions-controller's AddToFavorites() action.
- How is this accomplished?
- Is it now required to use
Html.ActionLink("add_to_favorites")
instead ofHtml.ActionLink("AddToFavorites")
to make the links in the HTML point asquestions/add_to_favorites
instead ofQuestions/AddToFavorites
?
Edit: Similar posts
- http://stackoverflow.com/questions/878578/how-can-i-have-lowercase-routes-in-asp-net-mvc
- http://stackoverflow.com/questions/1417389/asp-net-mvc-get-lowercase-links-instead-of-camel-case
One way to support underscores is to use the ActionName attribute:
[ActionName("add_to_favorites")]
public ActionResult AddToFavorites() {
// ...
}
However, this doesn't work for controllers. Perhaps if we could somehow remove all the underscores from the request before it gets to the routing mechanism, then it would work.