ASP. NET has no magic-ASP. net mvc routing matching and processing, asp. netmvc

Source: Internet
Author: User

ASP. NET has no magic-ASP. net mvc routing matching and processing, asp. netmvc

ASP. net mvc routing is the core of MVC applications and the entry to MVC application processing. As a developer, what you need to do normally is to define the entity and business logic as needed, then, it is displayed in the MVC Controller call and View. "routing" only defines a route table so that when you click a link, the application can hit the correct Controller, correct Action, and get the correct parameters so that the program can run normally.

However, ASP. NET has no magic. Why can an HTTP request be recognized as an MVC request by ASP. NET, instead of a Web Page or static resource file? How is the MVC Controller "correctly selected" and executed? This chapter will explain the following aspects:

● Main participants of ASP. NET routes
● ASP. net mvc route Registration
● UrlRoutingModule & MvcRouteHandler
● MvcHandler

Main participants of ASP. NET routes

I have previously introduced that an ASP. NET application has a global route table and is of the RouteCollection type under the System. Web. Routing namespace:

  

The Code defines three important objects: RouteBase, RouteData, and VirtualPathData. Other RouteValueDictionary objects are included in RouteData, route inherits from RouteBase, so it is introduced in three types directly.

1. RouteBase & Route:

Route is the core object of the entire routing mechanism. When registering a Route, this object is registered in the routing table. The following is the definition of RouteBase:

  

Route definition:

  

Its core methods are GetRouteData and GetVirtualPath, and the parameters are HttpContextBase and RequestContext. In other words, the role of the Route object is to capture data in the request context. And return the RouteData and VirtualPathData types.

Route only adds some additional attributes to its base class, such as Constraints, DataTokens, ults, and Route Handler. Both Constraints and ults can be passed in as parameters by registering a route, while RouteHandler and other content are hardcoded, such as System. web. the Extension Method MapRoute In the Mvc namespace:

  

IRouteHandler is the processor for each route, and the MVC Route Processor is created when the route is registered:

  

2. RouteData:

    

RouteData is used to wrap information about a route:
● DataTokens: contains a custom key-Value Pair dictionary, which will be uploaded to RouteHandler for use (as shown in the decompiled MapRoute method, the namespace information is stored in the DataTokens), but the data in this dictionary is not involved in route matching.
● Route: A Route object. RouteData is obtained through the GetRouteData of the Route object. The Route object here is the Route object that obtains RouteData.
● RouteHandler: The processor that processes the current request. The default value in MVC is MvcRouteHandler.
● Values: it is also a key-Value Pair dictionary, but it only contains URL parameters and default Values. It will participate in route matching (this explains why there is no action in the url template when registering a route, but if the default parameter exists, the default action can be routed ).

GetRequiredString: This method is used to pass in a string, and then use this string to check whether the Key exists in the Values attribute. If so, the return value is returned. Otherwise, System. InvalidOperationException is thrown. For example, the Controller and Action must exist in MVC. If the Action does not exist, an exception is thrown.

The following is the creation code of RouteData. We can see that all the attributes of RouteData are used to create its Route object:

  

3. VirtualPathData:

  

It indicates a virtual path and contains route information. The virtual path is the result of replacing the variable placeholder with the variable placeholder through the variables in RouteData. Values in the URL template. DataTokens works the same as the DataTokens in RouteData.

The following code creates VirtualPathData:

  

ASP. net mvc route Registration

Route registration is the process of adding a Route object to a Route table. The following code is the MVC Route registration code. Its core is to set the Route Processor to MvcRouteHandler.

  

The ASP. net mvc program has two route registration steps, which are completed in the Application_Start () method:

  

1. Call the AreaRegistration. RegisterAllAreas method to find all types inherited to AreaRegistration in all reference sets, and call the RegisterArea method of this type to register the routes in the routing table.

2. Call RouteConfig. RegisterRoutes to register the route information defined in the project to the routing table.

UrlRoutingModule

In the previous article, I introduced that IIS uses HttpModule to expand pipelines for ASP. NET programs (ASP. NET has no magic-How ASP. NET MVC runs? What is its lifecycle ?), The routing processing is actually a Module, and the UrlRoutingModule is the Module responsible for processing the routing, which is in systemroot \ Microsoft. NET \ Framework \ versionNumber \ Config \ web. the config file is registered in the pipeline:

  

Processing logic of UrlRoutingModule:

  

1. Match the Route. The GetRouteData method of RouteCollection actually traverses all the Route objects and then calls the GetRouteData method of the Route object. If no null is returned, the surface match is successful.

2. Get RouteHandler through Route.

3. Get HttpHandler through RouteHandler.

4. remap requests to HttpHandler.

According to the above analysis, points 1 and 2 in the above process show that the url-matched MVC route contains an MvcRouteHandler. What is the HttpHandler obtained through MvcRouteHandler?

MvcRouteHandler

According to the name, MvcRouteHandler is the MVC routing processor. According to the above analysis, the most important route processor is to obtain an HttpHandler. What is the HTTP processor obtained by MVC?

  

The code is very simple. It only processes Session behaviors (sessions in MVC will be introduced in subsequent chapters), and then creates an IHttpHandler type named MvcHandler.

MvcHandler

MvcHandler implements IHttpHandler and IHttpAsyncHandler, which respectively represent the processor for synchronous and asynchronous processing of Http requests in ASP. NET. The intent is obviously used to process mvc http requests.

The following describes how MvcHandler synchronizes requests:

    

The above code has done the following:
1. initialize controller and controllerFactory Based on httpContext.
2. Execute controller.
3. Release the controller.

Summary

  This chapter mainly analyzes and introduces the important objects and methods related to routing. The whole process is as follows:
1. register the route and add all route information to the RouteCollection type routing table.
2. When an Http request is processed by the UrlRoutingModule, The UrlRoutingModule matches and returns the hit Route object through the GetRouteData method of RouteCollection.
3. Then, UrlRoutingModule obtains the actual IRouteHandler type (MvcRouteHandler in MVC) through the Route object ).
4. Finally, the UrlRoutingModule obtains the actual IHttpHandler through the actual IRouteHandler type (MvcHandler in MVC) and forwards the request to IHttpHandler for processing.
5. MvcHandler initializes the Controller Based on the Http context and completes the execution.
  
Now we have learned how an HTTP request is routed to MvcRouteHandler and forwarded to MvcHandler for processing. MvcHandler has completed the creation, execution, and destruction of the Controller, in the future, we will further analyze the relationship between MvcHandler and routing and how to initialize and execute the process.

Address: http://www.cnblogs.com/selimsong/p/7661399.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.