How to route lower-case URLs ('questions/add_to_favorites/123') with underscores in ASP.NET MVC2?

You can add custom routes manually. This is not an universal solution and must be added for every controller and action separately.

Up vote 3 down vote favorite share g+ share fb share tw.

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: stackoverflow.com/questions stackoverflow.com/users/377920/randomguy reddit.com/ad_inq/ 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 of Html. ActionLink("AddToFavorites") to make the links in the HTML point as questions/add_to_favorites instead of Questions/AddToFavorites? Edit: Similar posts stackoverflow.com/questions/878578/how-can-i-have-lowercase-routes-in-asp-net-mvc 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. Asp. Net-mvc-2 asp.

Net-routing link|improve this question edited Jun 29 '10 at 23:41 asked Jun 29 '10 at 1:17randomguy1,7421527 72% accept rate.

Quick side note. Please ensure that add_to_favorite requires POST. GET request should never modify state on the server – Earlz Jun 29 '10 at 23:42 Agreed!

This is seriously not only for RESTful purists: state changing GET requests create security holes. Although, if you force authorization and prevent cross-site request forgery (using AntiForgeryToken), I don't think GET'ing is a problem technically speaking. – randomguy Jun 29 '10 at 23:48 And please note that simply adding HttpPost won't solve the security issues; the bad guy could simply emulate a form post.

So even though you are forcing POSTs (or DELETEs/PUTs from AJAX), you still have be careful. – randomguy Jun 29 '10 at 23:56.

You can add custom routes manually. This is not an universal solution and must be added for every controller and action separately. Routes.

MapRoute( "Web2.0 RoR style lowercase URLs with underscores", "questions-foo/add_to_favorites", new { controller = "Questions", action = "AddToFavorites" } ); The cool thing is that the URL generating Html-helper methods don't need to be modified. The routing table is used to route incoming requests and to generate URLs. So, Html.

ActionLink("Add to favorites", "Questions", "AddToFavorites"); maps to /questions-foo/add_to_favorites. Note that the original /Question/AddToFavorites still works as does /qUeStIoN/aDdtOfAvOrItEs as well as /qUeStIoNs-FOO/ADD_TO_FAVORITES because the default routing mechanism is case-insensitive.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions