In this article, we will mainly discuss how UrlRoutingModule intercepts the Http applicatioin pipeline events and introduce Http requests into the Mvc framework. To analyze this process, we need to use the source code of the UrlRoutingModule class.
Now we can download the. Net4.0 source code from Microsoft's official website.
: Http://aspnet.codeplex.com/releases/view/58781
First, we will generate an MvcApplication class in the Global. asax file when generating an Asp.net MVC3.0 project. In this class, we can register our defined routing rules in the Application_Start method.
MvcApplication. cs
1 public class MvcApplication: System. Web. HttpApplication
2 {
3 public static void RegisterGlobalFilters (GlobalFilterCollection filters)
4 {
5 filters. Add (new HandleErrorAttribute ());
6}
7
8 public static void RegisterRoutes (RouteCollection routes)
9 {
10 routes. IgnoreRoute ("{resource}. axd/{* pathInfo }");
11
12 routes. MapRoute (
13 "Default", // Route name
14 "{controller}/{action}/{id}", // URL with parameters
15 new {controller = "Home", action = "Index", id = UrlParameter. Optional} // Parameter defaults
16 );
17
18}
19 protected void Application_Start ()
20 {
21 AreaRegistration. RegisterAllAreas ();
22
23 RegisterGlobalFilters (GlobalFilters. Filters );
24 RegisterRoutes (RouteTable. Routes );
25}
26}
27
Let's take a look at the routes. MapRoute method implementation. In this method, we add our custom routing rules to the RouteTable. Routes global routing table.
RouteCollectionExtensions. cs
In this case, we need to note that the MvcRouteHandler object is passed in when the Route object is created. When will this object be used? We need to check the source code of UrlRoutingModule.
UrlRoutingModule. cs
Www.2cto.com
We can see that when the UrlRoutingModule initializes and calls the Init method, it registers the PostResolveRequestCache pipeline event of HttpApplication. Therefore, when the HttpAplication object (MvcApplication) is executed, the PostResolveRequestCache event is triggered, so as to direct HttpRequest into the MVC module, let's take a look at how Mvc handles the Request.
We can see that the PostResolveRequestCache method includes:
RouteData routeData = RouteCollection. GetRouteData (context );
GetRouteData is used to find the RouteData that matches the current request URL.
RouteCollection. cs
Bytes -------------------------------------------------------------------------------------------------
Next we can see:
IRouteHandler routeHandler = routeData. RouteHandler;
IHttpHandler httpHandler = routeHandler. GetHttpHandler (requestContext );
Context. RemapHandler (httpHandler );
The above code is to obtain the routing rules we have registered in the Application_Start method, find the MvcRoutingHandler, and then call the GetHttpHandler method.
Get IHttpHandler.
The following logic is:
1. Register HttpHandler in IIS 7 to the workprocesser of IIS.
2. Put HttpHandler in iis6.
Finally, run HttpHandler after HttpApplication. PreRequestHandlerExcute.
HttpContext. cs/Refer to HttpContext. RemapHandler method.
Bytes -----------------------------------------------------------------------------------------------
Next let's take a look at the MvcRoutingHandler source code. We can see that the GetHttpHandler method finally returns the MvcHandler object.
HttpContext. Handler object. The Handler. ProcessRequest method will be called to process the HttpRequest request.
MvcRoutingHandler. cs
Bytes ----------------------------------------------------------------------------------------------
The ProcessRequestInit method is called in the MvcHandler. ProcessRequest method, and ControllerBuilder. GetControllerFactory () is used here ();
So far, the IIS request enters the Mvc processing process. For the next Service Location work, please read my other article.
DependencyResolver and Service Location: http://www.bkjia.com/kf/201112/113066.html
Thank you.
From the rain of November