Add routing priority to ASP. net mvc and WebApi (1)

Source: Internet
Author: User

Add routing priority to ASP. net mvc and WebApi (1)

I. Why do I need a route priority?

We all know that we are in Asp. net MVC project or WebApi project, the registered route has no priority. when the project is large, or has multiple regions, or multiple Web projects, or is developed using a plug-in framework, our route registration is probably not written in a file, but distributed in many files of different projects. As a result, the priority of the route is highlighted.

For example: App_Start/RouteConfig. cs

 
 
  1. Routes. MapRoute (
  2. Name: "Default ",
  3. Url: "{controller}/{action}/{id }",
  4. Defaults: new {controller = "Home", action = "Index", id = UrlParameter. Optional}
  5. );
  6.  
  7. Areas/Admin/AdminAreaRegistration. cs
  8.  
  9. Context. MapRoute (
  10. Name: "Login ",
  11. Url: "login ",
  12. Defaults: new {area = "Admin", controller = "Account", action = "Login", id = UrlParameter. Optional },
  13. Namespaces: new string [] {"Wenku. Admin. Controllers "}
  14. );

If you first register the above general default route and then register the login route, then in any case, the first qualified route will be matched first, that is, the registration of the second route is invalid.
The reason for this problem is the order of registration of the two routes, and Asp. net MVC and WebApi do not have the concept of priority. Therefore, we need to implement this idea on our own today and add a concept of priority when registering a route.

Ii. Solution

 
 
  1. 1. Analyze the ingress of Route registration. For example, we create a project named mvc4.0.
  2.  
  3. Public class MvcApplication: System. Web. HttpApplication
  4. {
  5. Protected void Application_Start ()
  6. {
  7. AreaRegistration. RegisterAllAreas ();
  8.  
  9. WebApiConfig. Register (GlobalConfiguration. Configuration );
  10. FilterConfig. RegisterGlobalFilters (GlobalFilters. Filters );
  11. RouteConfig. RegisterRoutes (RouteTable. Routes );
  12. }
  13. }

There are two entry points for registering Mvc routes:
A. AreaRegistration. RegisterAllAreas (); register a region route
B. RouteConfig. RegisterRoutes (RouteTable. Routes); Register the project route

There is a WebApi route registration entry:
WebApiConfig. Register (GlobalConfiguration. Configuration); Register a WebApi route

2. Process Analysis of registered routes

AreaRegistrationContext
RouteCollection
HttpRouteCollection

When registering a route, these three classes are used to register and process the route.

3. Route Priority Scheme

A. Change the ingress of Route Registration
B. Customize the RoutePriority and HttpRoutePriority of a route. Both classes have the Priority attribute.
C. Customize a RegistrationContext to register the route. The registered object is the preceding custom route.
D. After all the routes are registered, they are added to the RouteCollection and HttpRouteCollection in priority order.


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.