The process of processing requests by ASP.
- inetinfo.exe: The WWW service process, IIS services, and Aspnet_isapi.dll are both hosted in this process.
- aspnet_isapi.dll: The Win32 component that handles. aspx files. In fact, the IIS server only recognizes. html files, and when the requested file is found to be an. aspx file, the IIS server gives it to Aspnet_isapi.dll to handle.
- aspnet_wp.exe process: The ASP. NET Framework process, which provides a managed environment in which the. NET runs, the CLR (the common language runtime) is parked in this process.
The ASP. NET Framework processes an Http Request:
Httprequest→inetinfo.exe→aspnet_isapi.dll→aspnet_wp.exe→httpruntime→httpapplication factory→httpapplication→ HttpModule →httphandler factory→ httphandler →httphandler.processrequest ()
The ASP. NET request processing process is based on the pipeline model, and the HTTP request is routed to each HttpModule in the pipeline, which is finally processed by HttpHandler, and once the processing is completed, it passes through the HttpModule in the pipeline and returns the result to the client. Note: During the processing of HTTP requests, only one httphandler can be called, but multiple httpmodule may be called.
The processing of requests can be intervened in each httpmodule. When the request arrives at HttpModule, the system does not actually process the request, but we can attach some additional information before the request is passed to the processing center (HttpHandler), intercept the request and do some extra work, or terminate the request. After HttpHandler finishes processing the request, we can also return the client after processing the result of the request processing again in the corresponding HttpModule.
HttpModule
HttpModule is the class that implements the System.Web.IhttpModule interface. Declaration of the IHttpModule interface:
Public Interface IHttpModule
{
Gives the HTTP module an opportunity to clean up before the object is garbage collected, typically without writing code.
void Dispose ();
Automatic invocation at initialization, where you can register your own event handlers with events in the HttpApplication object
void Init (HttpApplication context);
}
HttpModule can register the following sequence of events with the System.Web.HttpApplication object (the event firing order is sorted):
- application_beginrequest: triggered when the ASP. NET runtime receives a new HTTP request.
- application_authenticaterequest: triggered when the ASP. NET runtime is ready to authenticate the user.
- application_authorizerequest: triggered when the ASP. NET runtime is ready to authorize users to access resources.
- Application_resolverequestcache: We raise this event to decide whether we can use the content returned from the output cache to end the request. However, this depends on how the output cache of the Web application is set.
- application_acquirerequeststate: This event is raised when the ASP. NET runtime is ready to receive the session state of the current HTTP request.
- Application_prerequesthandlerexecute: This event is raised before ASP. NET starts executing a handler for the HTTP request. After this event, ASP. NET forwards the request to the appropriate HTTP handler.
- Application_postrequesthandlerexecute: This event is raised when the HTTP handler finishes execution.
- application_releaserequeststate: This event is raised when ASP. NET ends all request handler execution.
- application_endrequest: This event is raised before the response content is sent to the client.
- application_presendrequestheaders: This event is raised before ASP. NET sends HTTP response header information to the client. This event allows us to change the contents of the header before it reaches the client. We can use this event to add cookies and custom data to the header information.
- application_presendrequestcontent: This event is raised before ASP. NET sends the response content to the client. This event allows us to change the response content before the content arrives at the client. We can use this event to add content to the page output for all pages. Examples include generic menus, header information, foot information, or some comments and tags.
The rest, as well as the Error event (triggered when an unhandled exception occurs during the processing of an HTTP request), thedeposed event (triggered when ASP. NET finishes processing the HTTP request) . The Updaterequestcache event, which is triggered when ASP. NET finishes processing the current HTTP request and the output is ready to be added to the output cache. This depends on how the output cache for the WEB application is set up.
HttpModule & HttpHandler life cycle graphs:
HttpModule using Example 1
Some sites each page will pop up an ad or on each page in an annotated form (<!---->) to add the site's copyright information. If you teach this kind of JS code on every page, writing it can be tedious, and changing and maintaining it will be very difficult.
HttpModule is a necessary way for a client to make a request to the client to receive a server response. We can add this comment text to the page text after the server finishes processing the request and before sending the response text to the client. In this way, each page request is appended with the comment text. So, in what event should this code be implemented? Any event between PostRequestHandlerExecute and Presendrequestcontent is possible, but it is common to write code in the EndRequest event, with the following steps:
- Add a custom pipeline class and implement the IHttpModule interface
- Registering the EndRequest event in the Init event to implement the event handling method
- Registering a custom HttpModule in Web.conofig
Public class Testmodule:ihttpmodule
{
Public void Dispose ()
{
}
Public void Init (HttpApplication context)
{
Context. EndRequest + = context_endrequest;
}
void context_endrequest (object sender, EventArgs e)
{
((HttpApplication) sender). Response.Write ("<!--company copyright--");
}
}
< httpmodules >
< Add name = "Testmodule" type = "Asp.net_web_form.util.testmodule,asp.net_web_form.util" />
</ httpmodules >
HttpModule using Example 2
When logging in, the login is successful, the user name is generally stored in the session, in each of the other pages of the Page_Load event to check whether the user name in the session, if it does not exist, it means that the user is not logged in, do not let it access the content. But this is too clumsy, almost every page to include the detection Session code, resulting in difficult to develop and maintain. Let's take a look at how to use HttpModule to reduce our workload. (If each page has a common parent class, such as BasePage, then detection is the same in the Load event of the parent class just write the detection code once)
Since the content in the Session is used here, we can only write code in the AcquireRequestState and PreRequestHandlerExecute events, because only those two events in HttpModule can access the session. Here you can choose to write code PreRequestHandlerExecute event, the implementation is relatively simple, do not paste code.
HttpHandler
HttpHandler is the processing center of the HTTP request, which actually compiles and executes the server page requested by the client, and appends the processed information to the HttpModule in the HTTP request stream. Unlike HttpModule, once you have defined your own HttpHandler class, its relationship to the system's HttpHandler will be "overwrite".
When configuring the HttpHandler section in Web. config, you can specify the verbs for HTTP (get,post), specify what resources are to be processed, and what HttpHandler to do.
In addition, if you want to use the Session in HttpHandler, the HttpHandler must be implemented IRequiresSessionState interface, the IRequiresSessionState interface is an empty interface, it does not have an abstract method, Just a labeled interface.
This article is referenced in the literature: http://www.cnblogs.com/yuanyuan/archive/2010/11/15/1877709.html
HttpModule & HttpHandler