MVC depends on System. Web. Routing to process Request Path parsing. That is to say, the process starts from System. Web. Routing. UrlRoutingModule.
Web. config
<HttpModules>
<Add name = "UrlRoutingModule" type = "System. Web. Routing. UrlRoutingModule, System. Web. Routing,..."/>
</HttpModules>
Then let's take a look at what the UrlRoutingModule has done internally.
Public class UrlRoutingModule: IHttpModule
{
Protected virtual void Init (HttpApplication application)
{
Application. PostResolveRequestCache + = new EventHandler (this. OnApplicationPostResolveRequestCache );
Application. PostMapRequestHandler + = new EventHandler (this. OnApplicationPostMapRequestHandler );
}
}
Two HttpApplication events are subscribed. In the lifecycle of ASP. NET applications, PostResolveRequestCache is triggered before PostMapRequestHandler, and finally IHttpHandler. ProcessRequest () is used to process the final request.
The request context is encapsulated for further processing.
Public class UrlRoutingModule: IHttpModule
{
Private void OnApplicationPostMapRequestHandler (object sender, EventArgs e)
{
HttpContextBase context = new HttpContextWrapper (HttpApplication) sender). Context );
This. PostMapRequestHandler (context );
}
Private void OnApplicationPostResolveRequestCache (object sender, EventArgs e)
{
HttpContextBase context = new HttpContextWrapper (HttpApplication) sender). Context );
This. PostResolveRequestCache (context );
}
}
First, PostResolveRequestCache is executed.
Public class UrlRoutingModule: IHttpModule
{
Public virtual void PostResolveRequestCache (HttpContextBase context)
{
RouteData routeData = this. RouteCollection. GetRouteData (context );
If (routeData! = Null)
{
IRouteHandler routeHandler = routeData. RouteHandler;
...
If (! (RouteHandler is StopRoutingHandler ))
{
RequestContext requestContext = new RequestContext (context, routeData );
IHttpHandler httpHandler = routeHandler. GetHttpHandler (requestContext );
...
RequestData data2 = new RequestData ();
Data2.OriginalPath = context. Request. Path;
Data2.HttpHandler = httpHandler;
Context. Items [_ requestDataKey] = data2;
Context. RewritePath ("~ /UrlRouting. axd ");
}
}
}
}
RouteCollection is actually a reference to RouteTable. Routes.
Public class UrlRoutingModule: IHttpModule
{
Public RouteCollection
{
Get
{
If (this. _ routeCollection = null)
{
This. _ routeCollection = RouteTable. Routes;
}
Return this. _ routeCollection;
}
Set
{
This. _ routeCollection = value;
}
}
}
Okay, I jumped again.
Public class RouteTable
{
Private static RouteCollection _ instance = new RouteCollection ();
Public static RouteCollection Routes
{
Get
{
Return _ instance;
}
}
}
- Four pages in total:
- Previous Page
- 1
- 2
- 3
- 4
- Next Page