Asp.net MVC5 series-Routing features (1)

Source: Internet
Author: User

Asp.net MVC5 series-Routing features (1)

ASP. net mvc 5 supports a new routing protocol called routing feature. As the name suggests, the routing feature uses features to define the path. The routing feature gives you more control over the Web application URI. Of course, MVC5 also supports the previously defined routing method. You can use both methods to define routing in a project.

Routing features

For example, an e-commerce website may have the following routing rules:

{productId:int}/{productTitle} Mapped to ProductsController.Show(int id){username} Mapped to ProfilesController.Show(string username){username}/catalogs/{catalogId:int}/{catalogTitle} Mapped to CatalogsController.Show(string username, int catalogId)

In the previous ASP. net mvc version, this rule will be set in the routeconfig. cs file, and the actual controller Action method will be pointed out, such:

 
 
  1. 1 routes.MapRoute( 
  2. 2     name: "ProductPage", 
  3. 3     url: "{productId}/{productTitle}", 
  4. 4     defaults: new { controller = "Products", action = "Show" }, 
  5. 5     constraints: new { productId = "\\d+" } 
  6. 6 ); 

When the routing definition and Action method are declared in the same source file rather than in the external configuration class, it can easily process the URI ing between the URI and the Action. The previous path definition uses the following features to achieve the goal:

 
 
  1. [Route("{productId:int}/{productTitle}")] 
  2. public ActionResult Show(int productId) { ... } 

Use routing features

First, you must enable Attribute routing. You can call the MapMvcAttributeRoutes method to enable Attribute routing:

 
 
  1. public class RouteConfig 
  2.  { 
  3.      public static void RegisterRoutes(RouteCollection routes) 
  4.     { 
  5.         routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 
  6.    
  7.         routes.MapMvcAttributeRoutes(); 
  8.     } 
  9.  } 

It can also be used with conventional route settings.

 
 
  1. public static void RegisterRoutes(RouteCollection routes) 
  2.    routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 
  3.  
  4.    routes.MapMvcAttributeRoutes(); 
  5.   
  6.    routes.MapRoute( 
  7.         name: "Default", 
  8.        url: "{controller}/{action}/{id}", 
  9.         defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } 
  10.     ); 

Optional default values of URI parameters and Parameters

You can add "?" Mark a parameter as an optional parameter, or set the default value (parameter = value) for the form parameter ).

 
 
  1.  1 public class BooksController : Controller 
  2.  2 { 
  3.  3     // eg: /books 
  4.  4     // eg: /books/1430210079 
  5.  5     [Route("books/{isbn?}")] 
  6.  6     public ActionResult View(string isbn) 
  7.  7     { 
  8.  8         if (!String.IsNullOrEmpty(isbn)) 
  9.  9         { 
  10. 10             return View("OneBook", GetBook(isbn)); 
  11. 11         } 
  12. 12         return View("AllBooks", GetBooks()); 
  13. 13     } 
  14. 14   
  15. 15     // eg: /books/lang 
  16. 16     // eg: /books/lang/en 
  17. 17     // eg: /books/lang/he 
  18. 18     [Route("books/lang/{lang=en}")] 
  19. 19     public ActionResult ViewByLanguage(string lang) 
  20. 20     { 
  21. 21         return View("OneBook", GetBooksByLanguage(lang)); 
  22. 22     } 
  23. 23 } 

In the preceding example,/books and/books/1430210079 are routed to the View method. The former returns the list information of all books, and the latter returns the information of specific books. /Books/lang and/books/lang/en will adopt the same method.

Route prefix

In the same controller, routes start with the same prefix. For example:

 
 
  1. 1 public class ReviewsController : Controller 
  2.  2 { 
  3.  3     // eg: /reviews 
  4.  4     [Route("reviews")] 
  5.  5     public ActionResult Index() { ... } 
  6.  6     // eg: /reviews/5 
  7.  7     [Route("reviews/{reviewId}")] 
  8.  8     public ActionResult Show(int reviewId) { ... } 
  9.  9     // eg: /reviews/5/edit 
  10. 10     [Route("reviews/{reviewId}/edit")] 
  11. 11     public ActionResult Edit(int reviewId) { ... } 
  12. 12 } 

You can use the [routeprefix] feature to set a common prefix for the entire controller:

 
 
  1. 1 [RoutePrefix("reviews")] 
  2.  2 public class ReviewsController : Controller 
  3.  3 { 
  4.  4     // eg.: /reviews 
  5.  5     [Route] 
  6.  6     public ActionResult Index() { ... } 
  7.  7     // eg.: /reviews/5 
  8.  8     [Route("{reviewId}")] 
  9.  9     public ActionResult Show(int reviewId) { ... } 
  10. 10     // eg.: /reviews/5/edit 
  11. 11     [Route("{reviewId}/edit")] 
  12. 12     public ActionResult Edit(int reviewId) { ... } 
  13. 13 } 

If you need to define different routing prefixes, you can use the Tilde in the method features ~ Overwrite the original prefix, for example:

 
 
  1. 1 [RoutePrefix("reviews")] 
  2. 2 public class ReviewsController : Controller 
  3. 3 { 
  4. 4     // eg.: /spotlight-review 
  5. 5     [Route("~/spotlight-review")] 
  6. 6     public ActionResult ShowSpotlight() { ... } 
  7. 7   
  8. 8     ... 
  9. 9 } 


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.