Link: http://blog.csdn.net/21aspnet/archive/2009/02/02/3857963.aspx
In the past ASP, when a *. ASP page file is requested, this httprequest will first be intercepted by an inetinfo.exe process, which is actually a WWW Service. After interception, the request is forwarded to the ASP. dll process, which interprets the ASP page and then returns the interpreted data stream to the client browser. In fact, ASP. dll is an ISAPI file attached to IIS. It is responsible for interpreting and executing files such as ASP files and ASA,
-------------------------------------
ASP. net http Request Handling Method
When the client requests a * WWW process interception (WWW Service) from the Web server and determines the file suffix, it forwards the request to aspnet_isapi.dll, and aspnet_isapi.dll uses an HTTP pipeline, send the HTTP request to the aspnet_wp.exe process. After the HTTP request enters the aspnet_wp.exe process, the Asp.net Framework processes the HTTP request through httpruntime and then returns the result to the client.
------------------------------------
After an HTTP request is sent to httpruntime, the request will be sent to a container called httpapplication factory, the container will provide an httpapplication instance to process the passed HTTP requests, and then the HTTP requests will enter the following containers in sequence:
Httpmodule --> httphandler factory --> httphandler
After the httphandler's processrequest method in the system is processed, the entire HTTP request is processed and the client obtains the corresponding information.
Complete HTTP request processing process in Asp.net framework:
Httprequest --> inetinfo.exe-> pipeline --> HTTP pipeline --> aspnet_wp.exe --> httpruntime --> httpapplication factory --> httpapplication --> httpmodule --> httphandler factory --> httphandler. processrequest ()
If you want to intercept an httprequest in the middle and perform some processing on your own, you should implement this internally during the httpruntime runtime. Specifically, you should implement this in the httpmodule container..
----------------------------------------
The system's httpmodule implements an ihttpmodule interface. Of course, our own class can also implement the ihttpmodule interface, which can replace the system's httpmodule object.
The default httpmodule in ASP. NET:
Defaultauthenticationmodule ensures that the authentication object exists in the context. This class cannot be inherited.
Fileauthorizationmodule verifies that the remote user has the NT permission to access the requested file. This class cannot be inherited.
Formsauthenticationmodule enables ASP. NET ApplicationsProgramTo use forms for authentication. This class cannot be inherited.
The passportauthenticationmodule provides packaging for the passportauthentication service. This class cannot be inherited.
Sessionstatemodule provides session status services for applications.
Urlauthorizationmodule provides URL-based authorization services to allow or deny access to specified resources. This class cannot be inherited.
Windowsauthenticationmodule enables ASP. NET Applications to use Windows/IIS authentication. Unable to inherit this class
--------------------------------------
The default httpmodule of these systems is in the file machine. the configuration in config and the Web. the Config relationship is in ASP. when the Net Framework starts to process an HTTP request, it will load the machine in sequence. the web. config file. If an httpmodule is configured in the machine, you can still. remove the ing from the config file.
You can create a class library project in:
Using System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. text;
Using System. Web;
namespace lib
{< br> Public class helloworldmodule: ihttpmodule
{< br> Public helloworldmodule ()
{< BR >}
PublicString modulename
{
Get{Return "Helloworldmodule";}
}
// In the init function, register for httpapplication
// Events by adding your handlers.
Public Void Init (httpapplication Application)
{
Application. beginrequest + =
( New Eventhandler ( This . Application_beginrequest ));
Application. endrequest + =
( New Eventhandler ( This . Application_endrequest ));
}
Private Void Application_beginrequest (Object source,
Eventargs E)
{
// Create httpapplication and httpcontext objects to access
// Request and response properties.
Httpapplication = (Httpapplication) source;
Httpcontext Context = Application. context;
Context. response. Write ( " <H1> <font color = Red> helloworldmodule: Beginning of request </font> " );
}
Private Void Application_endrequest (Object source, eventargs E)
{
Httpapplication = (Httpapplication) source;
Httpcontext Context = Application. context;
Context. response. Write ( " <HR> " );
}
Public VoidDispose ()
{
}
}
}
Create a web project, add a reference to the above class library project, and modify web. config <Add name = "helloworldmodule" type = "Lib. helloworldmodule"/>
</Httpmodules> run the default. ASPX page of the web project to check the effect.
------------------------------------------------------------------ Go deep into httpmodule
After being captured by ASP. NET Framework, an HTTP request is sent to httpmodule and httphandler for processing in sequence. HM and HH are not completely independent. In fact, HTTP requests transfer control to HH in an event during HM transfer, while The real processing is executed in httphandler (that is,Processrequest Method) After completion, httphandler will return the control to the httpmodule again.
The above Code The init () parameter of httpmodule in is of the httpapplication type. It has many events, including beginrequest, endrequest, and authentiacterequest.
-----------------------------------------------------------------
Ihttphandler
It is an interface provided by the Asp.net framework and defines some system conventions that must be implemented if an HTTP request is to be processed. That is to say, if you want to handle certain types of HTTP request information flows by yourself, you need to implement these system conventions. For example, if a *. aspx file is used to process HTTP requests of this type, ASP. NET Framework will be handed over to an httphandler class named system. Web. UI. pagehandlerfactory for processing.
Like hm, the system uses ASP. NET framework first loads machine. httphandler in config, and then load the web in the directory where the web application is located. the custom httphandler class in config. HoweverThe relationship between the system and our custom hh is "Overwrite", that is, if we define a custom "*. if the httphandler class of aspx is used, the system will assign the HTTP request processing permission to the httphandler class defined by us, our own httphandler class needs to completely parse the HTTP request and process it.
Processrequest is the most important method in the ihttphandler interface. This method is used by httphandler to process an HTTP request. When an HTTP request is passed to the httphandler container by the httpmodule container, the Framework calls the httphandler's processrequest method to process the HTTP request.
In fact, the Framework does not directly locate the HTTP request of the relevant page on an internal default ihttphandler container, but on its internal default ihttphandler factory. Ihttphandler factory is used to schedule and manage the ihttphandler containers that have been implemented by many systems. This greatly enhances the load and efficiency of the system.