Transfer from http://www.cnblogs.com/darrenji/p/3795676.html
In the previous article, "19 key links (1-6) of the ASP. NET MVC request processing pipeline life cycle ", this article continues with 1-6 key steps.
⑦ httpruntime Create a HttpContext object based on the Isapiworkerrequest object
⑧httpapplicationfactory create a new or get an existing, available HttpApplication object from the HttpApplication pool
HttpApplication's work includes:
Load all HttpModule when initializing
Receiving requests
Different events are raised at different stages, allowing HttpModule to subscribe to events to be added to the requested process
Obtain a IHttpHandler instance at a specific stage, and eventually submit the request to a specific ihttphandler to implement
⑨ Next, that's when httpmodules plays a role.
All httpmodules implement the IHttpModule interface:
Interface ihttpmodule{ void Init (HttpApplication app); void Dispose ();}
As can be seen, Httomodules is the Init method that subscribes to all events of HttpApplication based on the passed-in HttpApplication type parameter.
We customize a HttpModule:
PublicClasstestmodule:ihttpmodule{PublicvoidDispose () {}Publicvoid Init (HttpApplication app) {app. Postacquirerequeststate + = new EventHandler (app_ Postacuiredrequeststate); App. PreRequestHandlerExecute + = new EventHandler (app_ PreRequestHandlerExecute); } void App_prerequesthandlerexecute (object sender, EventArgs e) {//todo:} void app_postacquiredrequeststate (object sender, EventArgs e) {//todo: }}
⑩ when a request matches a rule, ASP. NET calls the GetHandler method of the matching httphandlerfactory to get a HttpHandler instance, and finally a HttpHandler instance to process the current request, generating the response content
All httphandlers Implement the IHttpHandler interface:
Interface ihttphandler{ bool isreusable{getvoid ProcessRequest (HttpContext context);}
For example, we can customize a HttpHandler to respond to a specific type of request:
PublicClasslogin:ihttphandler{PublicvoidProcessRequest (HttpContext context) {context. Response.ContentType ="Text/plain";String username = context. request.form["Name"];String Password = context. request.form[ "password" ]; if (Password=sth< Span style= "color: #800000;" > "" {System.Web.Security.FormsAuthentication.SetAuthCookie (username, false); Context. Response.Write ( "ok" Span style= "color: #000000;" >); } else {context. Response.Write ( " username and password are incorrect "
The entrance to ⑾asp.net MVC is in UrlRoutingModule, which is the 7th pipeline event Postresolverequestcahce subscribed to HttpApplication, in other words, The request was intercepted at the 7th pipeline event in Htttpapplication.
Urlroutemodlue implements the IHttpModule:
PublicClassurlroutingmodule:ihttpmodule{//FieldsPrivateStaticReadOnlyObject _contextkey =NewObject();PrivateStaticReadOnlyObject _requestdatakey =NewObject();PrivateRouteCollection _routecollection;//MethodsProtectedVirtualvoidDispose () {}ProtectedVirtualvoidInit (HttpApplication application) {if (application. Context.items[_contextkey] = =Null) {application. Context.items[_contextkey] =_contextkey; Application. Postresolverequestcache + =New EventHandler (This. Onapplicationpostresolverequestcache); } }Privatevoid Onapplicationpostresolverequestcache (Objectsender, EventArgs e) {HttpContextBase context =NewHttpcontextwrapper (((HttpApplication) sender). Context);This. Postresolverequestcache (context); } [Obsolete ("This method is obsolete. Override the Init method to use the Postmaprequesthandler event.")]PublicVirtualvoidPostmaprequesthandler (HttpContextBase context) {}PublicVirtualvoidPostresolverequestcache (HttpContextBase context) {Routedata Routedata =This. Routecollection.getroutedata (context);if (routedata! =Null) {Iroutehandler Routehandler =Routedata.routehandler;if (Routehandler = =Null) {ThrowNew InvalidOperationException (String. Format (CultureInfo.CurrentUICulture, SR. GetString ("Urlroutingmodule_noroutehandler"),Newobject[0])); }if (! ( RoutehandlerIsStoproutinghandler)) {RequestContext RequestContext =NewRequestContext (context, routedata); Context. Request.requestcontext =RequestContext; IHttpHandler HttpHandler =Routehandler.gethttphandler (RequestContext);if (HttpHandler = =Null) {ThrowNew InvalidOperationException (String. Format (CultureInfo.CurrentUICulture, SR. GetString ("Urlroutingmodule_nohttphandler"),NewObject[] {routehandler.gettype ()}); }if (HttpHandlerIsUrlauthfailurehandler) {if (!formsauthenticationmodule.formsauthrequired) {ThrowNew HttpException (0x191, SR. GetString ("Assess_denied_description3")); } urlauthorizationmodule.reporturlauthorizationfailure (HttpContext.Current,This); }Else{context. Remaphandler (HttpHandler); } } } }voidIhttpmodule.dispose () {This. Dispose (); }voidIhttpmodule.init (HttpApplication application) {This// Properties public< Span style= "color: #000000;" > RouteCollection routecollection {get {if (this._routecollection = = nullthis._routecollection = routetable.routes;} return this._routecollection;} set {this._routecollection =
UrlRoutingModule is configured in Web. config or the default Web. config:
<add name="UrlRoutingModule-4.0" type="System.Web.Routing.UrlRoutingModule"/> < /httpmodules>
⑿ and before the request arrives at UrlRoutingModule, we have configured the following in the global file
PublicClassMvcApplication:System.Web.HttpApplication {ProtectedvoidApplication_Start () {... Bundleconfig.registerbundles (Bundletable.bundles); } }PublicClassRouteconfig {PublicStaticvoidRegisterRoutes (RouteCollection routes) {routes. Ignoreroute ("{resource}.axd/{*pathinfo}"); Routes. MapRoute (name: "Default", url: "{controller}/{action}/{id}", defaults: new { Controller = "Home", action = "Index", id = urlparameter.optional});}}
This means that the route is registered to routecollection through the Maproute () method at the first pipeline event beginrequest of HttpApplication. In practice, UrlRoutingModule is getting routes through the static properties of RouteTable RouteCollection.
The 19 key segments of the ASP. NET MVC request processing pipeline life cycle include:
19 Key segments of the ASP. NET MVC Request processing Pipeline Lifecycle (1-6) ASP. 19 Key links in the lifecycle of the pipeline life cycle (7-12) ASP. 19 Key links to the lifecycle of the net MVC Request processing pipeline (13-19)