In the previous article, "ASP. 19 Key Links (1-6) for the pipeline life cycle," which has experienced 1-6 key points, this article continues.
⑦ 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:
Public 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:
Public classtestmodule:ihttpmodule{ Public voidDispose () {} Public voidInit (HttpApplication app) {app. Postacquirerequeststate+=NewEventHandler (app_postacuiredrequeststate); App. PreRequestHandlerExecute+=NewEventHandler (App_prerequesthandlerexecute); } voidApp_prerequesthandlerexecute (Objectsender, EventArgs e) { //TODO: } voidApp_postacquiredrequeststate (Objectsender, 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:
Public Interface ihttphandler{ bool isreusable{get;} void ProcessRequest (HttpContext context);}
For example, we can customize a HttpHandler to respond to a specific type of request:
Public classlogin:ihttphandler{ Public voidProcessRequest (HttpContext context) {context. Response.ContentType="Text/plain"; stringUsername = context. request.form["name"]; stringPassword = context. request.form["Password"]; if(password="sth") {System.Web.Security.FormsAuthentication.SetAuthCookie (username,false); Context. Response.Write ("OK"); } Else{context. Response.Write ("the user name 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:
Public classurlroutingmodule:ihttpmodule{// Fields Private Static ReadOnly Object_contextkey =New Object(); Private Static ReadOnly Object_requestdatakey =New Object(); Privateroutecollection _routecollection; //Methods protected Virtual voidDispose () {}protected Virtual voidInit (HttpApplication application) {if(Application. Context.items[_contextkey] = =NULL) {application. Context.items[_contextkey]=_contextkey; Application. Postresolverequestcache+=NewEventHandler ( This. Onapplicationpostresolverequestcache); } } Private voidOnapplicationpostresolverequestcache (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.")] Public Virtual voidPostmaprequesthandler (HttpContextBase context) {} Public Virtual voidPostresolverequestcache (httpcontextbase context) {Routedata Routedata= This. Routecollection.getroutedata (context); if(Routedata! =NULL) {Iroutehandler Routehandler=Routedata.routehandler; if(Routehandler = =NULL) { Throw NewInvalidOperationException (string. Format (CultureInfo.CurrentUICulture, SR. GetString ("Urlroutingmodule_noroutehandler"),New Object[0])); } if(! (Routehandler isStoproutinghandler)) {RequestContext RequestContext=NewRequestContext (context, routedata); Context. Request.requestcontext=RequestContext; IHttpHandler HttpHandler=Routehandler.gethttphandler (RequestContext); if(HttpHandler = =NULL) { Throw NewInvalidOperationException (string. Format (CultureInfo.CurrentUICulture, SR. GetString ("Urlroutingmodule_nohttphandler"),New Object[] {routehandler.gettype ()}); } if(HttpHandler isUrlauthfailurehandler) { if(!formsauthenticationmodule.formsauthrequired) {Throw NewHttpException (0x191, SR. GetString ("Assess_denied_description3")); } urlauthorizationmodule.reporturlauthorizationfailure (HttpContext.Current, This); } Else{context. Remaphandler (HttpHandler); } } } } voidIhttpmodule.dispose () { This. Dispose (); } voidihttpmodule.init (HttpApplication application) { This. Init (application); } //Properties Publicroutecollection routecollection {Get { if( This. _routecollection = =NULL) { This. _routecollection =routetable.routes; } return This. _routecollection; } Set { This. _routecollection =value; } }}
UrlRoutingModule is configured in Web. config or the default Web. config:
<add name="UrlRoutingModule-4.0 " type= " System.Web.Routing.UrlRoutingModule" />
⑿ and before the request arrives at UrlRoutingModule, we have configured the following in the global file
Public classMvcApplication:System.Web.HttpApplication {protected voidApplication_Start () {... Bundleconfig.registerbundles (Bundletable.bundles); } } Public classRouteconfig { Public Static voidregisterroutes (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.
Not to be continued ~ ~