Asp. NET no magic--asp.net MVC Direct-connect routing (attribute routing)

Source: Internet
Author: User

In the analysis previously created for the controller, it was known that the controller was created with two steps, namely the type lookup of the controller and the creation of a controller instance from the type.

When querying the type of a controller, it is actually done by the controller name and namespace in Routedata to match and find, and the controller name is based on the URL template to get , such as the default URL template: "{controller}/{action}/{id}". The following is a method for finding the controller type based on Controllername, and it is important to note that there is a judgment before the controller name lookup, when Routedata is not empty and is matched to a direct-attached route . Uses Routedata to obtain a controller type with additional logic. The code is as follows:

   

What is Directroute? This chapter describes the direct-connect routing related content from the following points:
What is direct-attached routing
How to use direct-connect routing in MVC
The difference between direct-attached routing and contract routing
Why use direct-connect routing

What is direct-attached routing

A direct-attach route is also known as an attribute route (Attribute Routing) because it is an attribute that applies the matching template of a route directly to the action method. If the action in the code needs to use Http://XXX/dr/test to access it normally:

  

The route attribute is defined as follows:

  

From its definition, you can see that the route feature can be applied to the controller and the action. The route attribute allows you to specify a route directly for the action.

So what is a direct-connect Route for a program ? From the code that was originally given, the code level is judged by the Routedata.hasdirectroutematch method, which is the judgment logic, which is very simple to simply determine if the value contains a key with the name "Ms_directroutematches". In other words, the routedata contains the key value of "Ms_directroutematches" is the direct-connect route.

  

How to use direct-connect routing in MVC

ASP. NET MVC has an extended object routecollectionattributeroutingextensions to the routing table, which, as shown, is the function of registering the route with the attribute definition in the current app into the route table .

  

So add the following code when registering the route:

  

Add the following test code to your project:

  

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

  

And the information for the Controller and action is included in the corresponding routing information:

  

  

Note: Three route information is added here, one of its values is a routecollectionroute type, that is, the routing table contains a routing table, the route information contained in the routing table is the program all the route attributes marked by the action generated , the other two linkgenerationroute are also routes generated through attributes. In other words, an attribute tag generates two routing information , but why an attribute tag is inserted into a routing object in two routing tables This section is no longer in-depth analysis.
For information on how to use default values, route prefixes, default routes, routing constraints, and area of direct-connect routing in direct-attached routes, refer to this article: https://blogs.msdn.microsoft.com/webdev/2013/10/17/ attribute-routing-in-asp-net-mvc-5/

The difference between direct-attached routing and contract routing

In relation to direct-attached routing, a route that uses a controller and an action is called a contract-based Route because the request URL carries the name of the agreed Controller and action. They differ mainly in the following points:
1. Direct-connect Routing and contract routing, where routing is registered differently , the former creates a route by locating whether the route attribute is used in all controller types. The latter is added by itself in the configuration file.
2. Direct connect routes include the corresponding controller and action information in the route object at registration , while the agreed route is to obtain controller and action information through URL template matching at parse time .
3. When the action is marked with the route attribute, the type lookup of the controller no longer finds the logic through the controller type of the contract-based route.
4. When the action is marked with the route attribute and a contract-based route is used in the project, the use of the contract-based routing method is not accessible to the tagged action, because the logic for finding the action is separate from their two routes:

  

5. The routedata in the Direct Connect routing request automatically adds the Ms_directroute related values as follows:

  

Why use direct-connect routing

The main feature of direct-connect routing is that the routing is in the same file as the actual code, which makes it easier to match the route to the actual action when the code is read, and the direct-attached route can "create" a more-compliant URL according to the actual demand. New convention, you can also avoid the exposure of contract-based routing to the internal implementation of the code.

Summary

This chapter introduces the concept of direct-connect routing through the discovery of two different controller and action methods in the MVC source code, which is actually a new feature added in MVC5, which can be used to specify the corresponding action's route template in the way of attributes. However, it is important to keep in mind that it is confusing to avoid the mixing of the two routing methods.

Reference:

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

This article link: http://www.cnblogs.com/selimsong/p/7722988.html

Asp. NET no magic--Directory

Asp. NET no magic--asp.net MVC Direct-connect routing (attribute routing)

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.