ASP. NET has no magic-ASP. net mvc direct connection routing (feature Routing), asp. netmvc

Source: Internet
Author: User

ASP. NET has no magic-ASP. net mvc direct connection routing (feature Routing), asp. netmvc

In the previous analysis on Controller creation, we learned that the creation of Controller consists of two steps: the type search of Controller and the creation of Controller instances based on the type.

When querying the Controller type, the Controller name and namespace in RouteData are used for matching and searching. The Controller name is obtained based on the url template in the routing object, for example, the default url template is "{controller}/{action}/{id }". The following is a method for finding the Controller type based on ControllerName. It should be noted that there is another judgment before the Controller Name Lookup. When RouteData is not empty and matched as a direct connection route, the system uses RouteData to obtain a Controller type. The Code is as follows:

   

What is DirectRoute? This chapter introduces the content of direct connection routing from the following points:
● What is a direct connection route?
● How to use direct connection routing in MVC
● Differences between direct connection routes and agreed routes
● Why do I use direct connection routing?

What is a direct connection route?

A directly connected route is also called Attribute Routing because it directly applies the matching template of the route to the Action method through the feature. For example, you must use http: // XXX/dr/test to access the action in the Code:

  

The Route feature is defined as follows:

  

From its definition, we can see that the Route feature can be applied to the Controller and Action. The Route feature allows you to directly specify routes for actions.

So what is a direct connection route for the program? From the initial code, the code layer is determined by the RouteData. HasDirectRouteMatch method, which is the judgment logic. It is very simple to determine whether the Value contains the Key Value named "MS_DirectRouteMatches. In other words, the value of the Key "MS_DirectRouteMatches" in RouteData is the direct connection route.

  

How to use direct connection routing in MVC

In ASP. net mvc, RouteCollectionAttributeRoutingExtensions, an extension object for the route table, is defined as shown in. Its function is to register the routes used in the current application to the routing table.

  

Therefore, add the following code when registering a route:

  

Add the following test code to the project:

  

After running the program, you can see that dr/test and dr/test2 are added to the routing table:

  

The corresponding routing information includes the Controller and action information:

  

  

Note: three routes are added here. One of them is of the RouteCollectionRoute type, which means that the route table contains another route table, the Route information in the included Route table is generated by all actions marked by the Route feature in the program. The other two LinkGenerationRoute are also routes generated by features. In other words, a feature tag generates two routes, but why does one feature tag insert route objects in both route tables.
For more information about how to use features such as default, prefix, default route, route constraints, and Area of a directly connected route in a directly connected route, see: https://blogs.msdn.microsoft.com/webdev/2013/10/17/attribute-routing-in-asp-net-mvc-5/

Differences between direct connection routes and agreed routes

Compared with a direct connection route, routes matching the Controller and Action are called convention-based routes, because the request url contains the name of the agreed Controller and Action. Their differences are as follows:
1. The registration method of the direct connection Route is different from that of the agreed Route. The former creates a Route by finding whether the Route feature is used in all Controller types. The latter can be added by yourself in the configuration file.
2. When registering a direct connection Route, the corresponding Controller and Action information is included in the Route object, and the agreed Route obtains the Controller and Action information through url template matching during parsing.
3. When an Action is marked by the Route feature, the type search logic of the Controller will no longer use the Controller type search logic based on the agreed Route.
4. when an Action is marked by the Route feature and the project uses a convention-based Route, the agreed-based routing method cannot be used to access the marked action, because the two routes in the Action search logic are separated:

  

5. The RouteData in the direct connection request automatically adds values related to MS_DirectRoute, as follows:

  

Why do I use direct connection routing?

The main feature of direct connection routing is that the routing and the actual code are in the same file. This makes it easier to match the routing with the actual Action when reading the code, in addition, direct connection routing can be used to "CREATE" a more compliant url "New Convention" based on actual needs. It can also avoid exposing the internal implementation of the code based on the agreed route.

Summary

This chapter finds two different Controller and Action processing methods in the source code of MVC to introduce the concept of direct connection routing. In fact, this is a new feature added to MVC5, you can use the feature to specify the routing template of the corresponding action. However, it should be noted that try to avoid code confusion caused by mixing of the two routing methods.

 

Refer:

Https://blogs.msdn.microsoft.com/webdev/2013/10/17/attribute-routing-in-asp-net-mvc-5/

Link: http://www.cnblogs.com/selimsong/p/7722988.html

ASP. NET has no magic-directory

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.