Asp. NET no magic--asp.net MVC route matching and processing

Source: Internet
Author: User

The route of ASP. NET MVC is a core of MVC application and the entry of MVC application processing, as a developer, under normal circumstances just need to define the entity, business logic, and then in the MVC controller to call, view to show, "Routing" Simply define a routing table so that when a user clicks on a link, the app can hit the correct controller, the correct action, and get the correct parameters to make the program work.

But there is no magic in ASP. Why is an HTTP request being recognized as an MVC request by ASP, not a Web page or a static resource file? How does MVC's controller be "properly selected" and executed? This chapter will explain the following aspects:

Primary participating objects for ASP.
Registration of ASP. NET MVC Routes
Urlroutingmodule&mvcroutehandler
Mvchandler

Primary participating objects for ASP.

Previously, there is a global routing table in the ASP. NET application and is the RouteCollection type under the System.Web.Routing namespace:

  

From this code definition you can find 3 important objects, namely RouteBase, Routedata, and Virtualpathdata, for the other routevaluedictionary are included in the Routedata, The route is inherited to RouteBase so it is introduced directly to the 3 types.

1. Routebase&route:

The route is the core object of the entire routing mechanism, which is registered in the routing table when the route is registered, and the following is the definition of routebase:

  

Definition of Route:

  

Its core approach is getroutedata and GetVirtualPath, and the parameters are HttpContextBase and RequestContext, in other words, thefunction of the route object is to crawl the data in the context of the request. and returns two types of Routedata and Virtualpathdata.

Route simply adds some additional attributes to its base class, such as constraints, Datatokens, Defaults, Route handler, and so on. and the constraints and defaults can be registered by the method of routing in the form of parameters, and Routehandler and other content is hard-coded, such as the SYSTEM.WEB.MVC namespace extension method Maproute:

  

Iroutehandler is the processor for each route, and the route processor for MVC is created when the route is registered:

  

2. Routedata:

    

Routedata is used to wrap information about the route:
Datatokens: Contains a custom key-value pair dictionary, which will eventually be used in Routehandler (as can be seen from the Maproute method of anti-compilation, the namespace information is placed inside the Datatokens), However, the data in this dictionary does not participate in route matching.
Route: Represents a Route object, Routedata is obtained through the getroutedata of the route object, where the route object is the route object that obtains Routedata.
Routehandler: The processor that handles the current request, which is mvcroutehandler by default in MVC.
Values: It is also a dictionary of key-value pairs, except that it contains only URL parameters and default values, and it participates in route matching (this explains why the URL template does not have an action when registering the route, but the default action can be routed to when the default parameter exists).

Getrequiredstring: The method is to pass in a string and then go through the string to find out if the key exists in the values property, or throw a System.InvalidOperationException exception if there is a return value. For example , the Controller and action must exist in MVC, and throws an exception if it does not exist .

The following is the creation code for Routedata, and you can see that the properties of Routedata are all for the route object used to create it:

  

3. Virtualpathdata:

  

It represents a virtual path and contains routing information, which is the result of a URL template replacing a variable placeholder with a variable in the routedata.values, and Datatokens is consistent with the Datatokens effect in Routedata.

Here's the code to create the Virtualpathdata:

  

Registration of ASP. NET MVC Routes

The registration of a route is the process of adding the route object to the routing table, which is the code for the MVC route registration, the core of which is to set the route's processor to Mvcroutehandler.

  

There are two steps in the route registration of the ASP. NET MVC program, both of which are done in the Application_Start () method:

  

1. Call the Arearegistration.registerallareas method to find all types that inherit to Arearegistration in all referenced assemblies . The Registerarea method of the type is called to register the route with the routing table.

2. Call the Routeconfig.registerroutes method to register the routing information defined in the project into the routing table.

UrlRoutingModule

In the previous article, IIS was introduced through HttpModule to complete the pipeline expansion of ASP. NET no magic--asp.net How does MVC work? What is its life cycle? ), and routing is actually a module,urlroutingmodule is responsible for handling the routing module, which is in Systemroot\microsoft. Net\framework\versionnumber\config The \web.config file is registered in the pipeline:

  

UrlRoutingModule's processing logic:

  

1. To match the route, RouteCollection's Getroutedata method actually iterates through all the route objects, then calls the Getroutedata method of the route object, and if the return is not NULL, the surface match succeeds.

2. Get Routehandler through the route.

3. Get HttpHandler through Routehandler.

4. Remap the request to HttpHandler.

In the above process, 1, 2 points according to the above analysis, the URL matching to the MVC route contains a mvcroutehandler, then the Mvcroutehandler obtained by the HttpHandler what is it?

Mvcroutehandler

By name, Mvcroutehandler is the processor of the MVC route, and the above analysis shows that the main route processor is to get a httphandler, so what is the HTTP processor that MVC gets?

  

The code is very simple, just dealing with the session behavior (about the session in MVC is covered in the next section), and then creating a IHttpHandler type named Mvchandler .

Mvchandler

Mvchandler implements the IHttpHandler and IHttpAsyncHandler, respectively, that represent the processors that synchronously and asynchronously process HTTP requests in ASP. The intent is clearly the HTTP request to handle MVC.

The following are the synchronization methods for Mvchandler processing requests:

    

The above code has done the following several things:
1. Initialize controller and Controllerfactory according to HttpContext.
2. Execute the controller.
3. Release the controller.

Summary

  This chapter mainly introduces the important objects and methods related to routing, and simply the whole process is:
1. Register the route and add all routing information to the RouteCollection type's routing table.
2. When the HTTP request is urlroutingmodule processed, UrlRoutingModule matches and returns the hit route object by RouteCollection's Getroutedata method.
3. UrlRoutingModule then obtains the corresponding Iroutehandler actual type through the route object (MVC is Mvcroutehandler).
4. Finally urlroutingmodule gets the actual IHttpHandler (Mvchandler in MVC) by iroutehandler the actual type, transferring the request to IHttpHandler for processing.
5. Mvchandler initializes the controller and completes execution based on the HTTP context.
  
Now that we understand 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. Subsequent analysis of the relationship between Mvchandler and routing and how to initialize and execute the process.

This address: http://www.cnblogs.com/selimsong/p/7661399.html

Asp. NET no magic--Directory

Asp. NET no magic--asp.net MVC route matching and processing

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.