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 routes.MapRoute(
- 2 name: "ProductPage",
- 3 url: "{productId}/{productTitle}",
- 4 defaults: new { controller = "Products", action = "Show" },
- 5 constraints: new { productId = "\\d+" }
- 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:
- [Route("{productId:int}/{productTitle}")]
- public ActionResult Show(int productId) { ... }
Use routing features
First, you must enable Attribute routing. You can call the MapMvcAttributeRoutes method to enable Attribute routing:
- public class RouteConfig
- {
- public static void RegisterRoutes(RouteCollection routes)
- {
- routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
-
- routes.MapMvcAttributeRoutes();
- }
- }
It can also be used with conventional route settings.
- public static void RegisterRoutes(RouteCollection routes)
- {
- routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
-
- routes.MapMvcAttributeRoutes();
-
- routes.MapRoute(
- name: "Default",
- url: "{controller}/{action}/{id}",
- defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
- );
- }
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 public class BooksController : Controller
- 2 {
- 3 // eg: /books
- 4 // eg: /books/1430210079
- 5 [Route("books/{isbn?}")]
- 6 public ActionResult View(string isbn)
- 7 {
- 8 if (!String.IsNullOrEmpty(isbn))
- 9 {
- 10 return View("OneBook", GetBook(isbn));
- 11 }
- 12 return View("AllBooks", GetBooks());
- 13 }
- 14
- 15 // eg: /books/lang
- 16 // eg: /books/lang/en
- 17 // eg: /books/lang/he
- 18 [Route("books/lang/{lang=en}")]
- 19 public ActionResult ViewByLanguage(string lang)
- 20 {
- 21 return View("OneBook", GetBooksByLanguage(lang));
- 22 }
- 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 public class ReviewsController : Controller
- 2 {
- 3 // eg: /reviews
- 4 [Route("reviews")]
- 5 public ActionResult Index() { ... }
- 6 // eg: /reviews/5
- 7 [Route("reviews/{reviewId}")]
- 8 public ActionResult Show(int reviewId) { ... }
- 9 // eg: /reviews/5/edit
- 10 [Route("reviews/{reviewId}/edit")]
- 11 public ActionResult Edit(int reviewId) { ... }
- 12 }
You can use the [routeprefix] feature to set a common prefix for the entire controller:
- 1 [RoutePrefix("reviews")]
- 2 public class ReviewsController : Controller
- 3 {
- 4 // eg.: /reviews
- 5 [Route]
- 6 public ActionResult Index() { ... }
- 7 // eg.: /reviews/5
- 8 [Route("{reviewId}")]
- 9 public ActionResult Show(int reviewId) { ... }
- 10 // eg.: /reviews/5/edit
- 11 [Route("{reviewId}/edit")]
- 12 public ActionResult Edit(int reviewId) { ... }
- 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 [RoutePrefix("reviews")]
- 2 public class ReviewsController : Controller
- 3 {
- 4 // eg.: /spotlight-review
- 5 [Route("~/spotlight-review")]
- 6 public ActionResult ShowSpotlight() { ... }
- 7
- 8 ...
- 9 }