In the application, you can specify the so-called routing attributes in the controller: [Route] & [RoutePrefix] .

Help to understand for what purposes it is customary to use this functionality.


As far as I could understand with the help of these attributes it is possible to build more conveniently readable url :

 public ActionResult GetDetailUserInfo(int userId){} 

in the absence of attributes url address will look like:

Controller Name / GetDetailUserInfo? UserId = 5

if you add the route attribute to the method [Route("{userId}/Details")]

then the url will look like this:

Controller Name / 5 / Details

Are there any other cases when you need to use these attributes?

    1 answer 1

    In asp.net mvc there are two options for specifying routes. Both can be configured the same, but there is a fundamental difference.

    The first option (you declare routes in the configuration, in one place):

     routes.MapRoute( name: “ProductPage”, url: “{productId}/{productTitle}”, defaults: new { controller = “Products”, action = “Show” }, constraints: new { productId = “\\d+” } ); 

    And the new more convenient and flexible way (recommended), based on the attributes of the controller. It allows you to decentralize the setting of routes and more intuitive.

    In the config:

     routes.MapMvcAttributeRoutes(); 

    In the controller:

     public class BooksController : Controller { // eg: /books // eg: /books/1430210079 [Route(“books/{isbn?}”)] public ActionResult View(string isbn) { if (!String.IsNullOrEmpty(isbn)) { return View(“OneBook”, GetBook(isbn)); } return View(“AllBooks”, GetBooks()); } // eg: /books/lang // eg: /books/lang/en // eg: /books/lang/he [Route(“books/lang/{lang=en}”)] public ActionResult ViewByLanguage(string lang) { return View(“OneBook”, GetBooksByLanguage(lang)); } } 

    Read more here .