Differences between HttpHandler and HttpModule

Source: Internet
Author: User

ASP. when the Http Request is processed by Net, the requests are processed by each HttpModule in the Pipeline (Pipeline) mode and then to HttpHandler. After the HttpHandler completes processing, the requests are still processed by each HttpModule in the Pipeline, finally, the HTML is sent to the client browser.
Several important objects are involved in the lifecycle: HttpHandler, HttpModule, and IHttpHandlerFactory. Their execution (sequence) is roughly like this: the client sends page requests, it is intercepted by a process in IIS. It is based on the requested page suffix (. different page handlers (. asp> asp. dll ;. aspx-> ISAPI. dll ). in the process of page processing, the page processing program must go through the process of HttpModule and HttpHandler: the former HttpModule is used to process some events before and after page processing, the latter HttpHandler handles real pages.
As mentioned above, HttpModule processes pages before and after page processing, so it does not affect real page requests.
It is often used to add some information (such as the copyright statement) to the header or tail of each page. I have seen some free space. After uploading the page, I found that,
There are a lot of Small advertisements in the header and tail of each page... if you understand the principle of HttpModule, it is not very difficult to do this ~

Differences between IHttpModule and IHttpHandler
1. sequence. IHttpModule first, and then IHttpHandler. Note: The Module depends on the event you have responded to. Some events run before Handler, and some run after Handler.
2. processing the request:
IHttpModule is a type of generic size. It is called no matter what file the client requests. For example, aspx, rar, and html requests.
IHttpHandler belongs to the picky eaters type. Only the file types registered by ASP.net (such as aspx and asmx) can call it.
3. IHttpHandler processes the request based on the content of the response generated by your request. The IHttpModule processes the request, such as verification, modification, and filtering. It can also process the response.
Asp.net event model Mechanism
I
The customer's request page is processed by the dynamic Connection Library aspnet_isapi.dll, which sends the requested aspx file to CLR for compilation and execution, and then returns the Html stream to the browser.
2. Page events
Execution sequence
Page_Init: initialization value or connection
Page_Load: mainly uses IsPostBack. This event mainly performs a series of operations to create an asp.net page or response for the first time.
Client events caused by shipping. The page and control view status have been restored before this event.
Page_DataBind: it can be called at the page level or in a single control.
DataBind_PreRender: Pre-rendering of data binding, which is triggered just before the view status and Rendering Control are saved.
Page_Unload: This event executes the final cleaning.
Uncertain event
Page_Error: if an unhandled exception occurs during page processing, an error event is triggered.
Page_AbortTransaction: Transaction event. If a transaction has been terminated during transaction processing, this event is triggered and is commonly used in shopping carts.
Page_CommitTransaction: this event is triggered if the transaction is successful.
Events in Global. asax (execution sequence)
Application_Start: triggered when the application starts
Application_BeginRquest: triggered when an http request starts.
Application_AuthenticateRequest: triggered when the application approves an http request
Session_Start: triggered when the session is started
Application_EndRequest: triggered when the Htttp request ends
Session_End: triggered when the session ends
Application_End: triggered when the application ends
Application_Error: triggered when an error occurs.
ISAPI: inserts some components into the web server to expand the functions and enhance the functions of the web server.
ISAPI: extended, win32 dynamic link library, such as aspnet_isapi.dll, can regard ISAPI extension as a common application, which processes HTTP requests.
ISAPI: filter. The web server sends the request to the related filter. Next, the filter may modify the request and perform some operations.
Processing of ASP. NET requests:
Based on the pipeline model, ASP. NET transmits http requests to all modules in the pipeline. Each module receives HTTP requests and has full control.
Once the request passes through all the HTTP modules, it is finally processed by the HTTP processing program. The HTTP handler processes the request and the result passes through the HTTP module in the module pipeline again.
Httpmodule
ISAPI filter: IIS does not support dynamic pages, that is, it only supports static HTML page content. For. asp. aspx. cgi. php,
IIS does not know that if these suffix tags are processed, it treats them as text and sends them to the client without any processing. To solve this problem, IIS has a mechanism called ISAPI filter. It is a COM component.
When the ASP. NET service registers to IIS, it registers the file extensions that each extension can process to IIS (for example, *. ascx *. aspx ).
After the extension is started, it processes the files that cannot be processed by IIS according to the defined method, and then redirects the control to the process that specifically processes the Code. In asp.net, It is aspnet_isapi.dll. Let this process start to process the code, generate standard HTML code, add the code to the original HTML, and finally return the complete HTML to IIS, IIS then sends the content to the client.
HttpModule
The Http module implements the ISAPI filter function. It is a. net component that implements the System. Web. IHttpModule interface.
These components register themselves in some events and insert themselves into the ASP. NET Request Processing pipeline. When these events occur, ASP. NET calls the HTTP module that is interested in the request,
This module can process the request. Sometimes you need to worry over the http request. Note that it does not overwrite other http modules that come with the system. The configuration is complete in Machine. config.
HttpHandler
It implements the ISAPI Extention function, which processes the Request information and sends the Response ). The IHttpHandler interface must be implemented through the HttpHandler function.
The HTTP handler is A. NET component that implements the System. Web. IHttpHandler interface. Any class that implements this interface can be used to process input Http requests. It is an Http processing program.
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 *. aspxpage file from the web server, the HTTP request is also intercepted by the inetinfo.exe process (www Service). After determining the file suffix,
Transfer this request to ASPNET_ISAPI.DLL, and ASPNET_ISAPI.DLL sends this Http request to the ASPNET_WP.EXE process through an http PipeLine,
After the HTTP request enters the ASPNET_WP.EXE process, the asp.net framework processes the Http request through HttpRuntime and returns the result to the client.
After an http request is sent to HttpRuntime, the Http 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 turn:
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-> ASPNET_ISAPI.DLL --> 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.

Original article address:

Http://www.sytm.net/jiejuefangan/k_20100619191534.html

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.