How to redirect lowercase urls ('questions / add_to_favorites / 123') with underscores in ASP.NET MVC2?

ASP.NET MVC 2 controllers and actions use UpperCamelCase.

For some reason, many large sites, including SO, use lowercase letters (underscores) for controllers and actions in URLs. Examples:

https://stackoverflow.com/questions
https://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 to do this.

By default, the router is case insensitive, i.e. stackoverflow.com/questions/askwill be redirected to Question-controller Ask () without any problems.

However, let's say we want to direct questions/add_to_favoritesAddToFavorites () to the action-controller.

  • How is this achieved?
  • Now you need to use Html.ActionLink("add_to_favorites") instead Html.ActionLink("AddToFavorites")to make links in the HTML point as questions/add_to_favoritesinstead Questions/AddToFavorites?

Edit: Related posts

- ActionName:

[ActionName("add_to_favorites")]
public ActionResult AddToFavorites() {
  // ...
}

. , - , , .

+3
1

. .

routes.MapRoute(
    "Web2.0 RoR style lowercase URLs with underscores", 
    "questions-foo/add_to_favorites", 
    new { controller = "Questions", action = "AddToFavorites" }
);

, URL-, Html-helper, . URL-. ,

Html.ActionLink("Add to favorites", "Questions", "AddToFavorites"); /questions-foo/add_to_favorites.

, /Question/AddToFavorites - , /Question/AddToFavorites, /questions-foo/add_to_favorites, .

+2

Source: https://habr.com/ru/post/1752181/


All Articles