Httpmodule, httphandler Introduction

Source: Internet
Author: User

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,
-------------------------------------

 

Brief Introduction to the implementation steps of Asp.net

 

1. IIS receives customer requests

2. IIS sends the request to aspnet_isapi.dll for processing.

3. (If the program is run for the first time) load the DLL in the bin directory

4. (If the program is run for the first time) read the configurations in webconfig at all levels

5. (If the program is run for the first time) Compile and load global. asax and initialize the httpapplication instance.

6. Create httpcontext in response to the request

7. Create an httptextwriter that carries the response results

8. Find the appropriate httphandler (Asp.net page) and process the HTTP request

9. Process session, exception

10. Feedback the processing results to customers

After an httpapplication instance is created,
The initmodules () method is called,
This method calls the corresponding httpmodule according to the configuration in the webconfig file.
This is the customizable httpmodule.

 

 

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 Applications to use forms 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.
Public class helloworldmodule: ihttpmodule
{
Public helloworldmodule ()
{
}

Public String 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 application = (httpapplication) source;
Httpcontext context = application. context;
Context. response. Write ("}

Private void application_endrequest (Object source, eventargs E)
{
Httpapplication application = (httpapplication) source;
Httpcontext context = application. context;
Context. response. Write ("<HR> }

Public void dispose ()
{
}
}

<System. Web>
<Httpmodules>
<Add name = "helloworldmodule" type = "helloworldmodule"/>
</Httpmodules>
</System. Web>
Bytes -----------------------------------------------------------------------------------
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.
In the above Code, the init () parameter of httpmodule 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. 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.
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.

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.