ASP. NET (httpmodule, httphandler)

Source: Internet
Author: User

This article from: 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 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.

Code
1 Public   Class Helloworldmodule: ihttpmodule
2 {
3 Public Helloworldmodule ()
4 {
5}
6
7 Public String modulename
8 {
9 Get   {Return "Helloworldmodule";}
10 }
11
12 // In the init function, register for httpapplication
13 // Events by adding your handlers.
14 Public   Void Init (httpapplication Application)
15 {
16 Application. beginrequest + =  
17 ( New Eventhandler ( This . Application_beginrequest ));
18 Application. endrequest + =  
19 ( New Eventhandler ( This . Application_endrequest ));
20 }
21
22 Private   Void Application_beginrequest (Object source, eventargs E)
23 {
24 // Create httpapplication and httpcontext objects to access
25 // Request and response properties.
26 Httpapplication = (Httpapplication) source;
27 Httpcontext Context = Application. context;
28 Context. response. Write ( " <H1> <font color = Red> helloworldmodule: Beginning of request </font> " );
29 }
30
31 Private   Void Application_endrequest (Object source, eventargs E)
32 {
33 Httpapplication = (Httpapplication) source;
34 Httpcontext Context = Application. context;
35 Context. response. Write ( " <HR> " );
36 }
37
38 Public   Void Dispose ()
39 {
40}
41 }
42
43

Web. config Configuration

<System. Web>
<Httpmodules>
<Add name = "helloworldmodule" type = "helloworldmodule"/>
</Httpmodules>
</System. Web>

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, after the real processing is completed in httphandler, httphandler will return the control to httpmodule again.
The aboveCodeThe 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, 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 HH and HM, 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. However, the relationship between the system and our custom hh is "Overwrite". That is to say, if we customize a policy "*. 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.
the most important method in the ihttphandler interface is processrequest. 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.
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.

Related Article

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.