ASP. net mvc 1.0 Process Analysis (System. Web. Routing)

Source: Internet
Author: User

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

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.