Recently received a few questions: what is the difference between HttpHandler and HttpModule, which one should I choose?
The reason for this is that the request can be accessed in these two classes of objects, and the response object can handle requests.
I thought I had done so many demos in my blog using ASP. NET to write my own service framework, but some people looked at my examples, still don't know how to choose them, and in order to achieve the same goal, I used HttpHandler, Also useful for HttpModule. It now seems that the examples I designed at that time were not a clear distinction between HttpHandler and HttpModule, but rather a demonstration of how to design a service framework using HttpHandler and HttpModule.
Fortunately that blog content did not overreaching, today had to write an article.
This article has agreed:
1. HttpHandler refers to all types that implement IHttpHandler interfaces.
2. HttpModule refers to all types that implement IHttpModule interfaces.
Therefore, these types and interfaces are not specifically distinguished from this article.
Back to the top understanding ASP.
HttpHandler and HttpModule, they are all related to the ASP, so I want to understand that these two types of objects must understand how the ASP. NET pipeline works.
Reflects the process flow of the ASP:
This is a time series diagram, we should understand it from two angles:
1. What are the call actions?
2. What are the participants?
Each invocation action reflects the processing phase of the ASP, which raises the corresponding events (in addition to Gethandler,processrequest), and HttpModule subscribes to the events and participates in the pipeline process. Some of these phases also raise two events, and a complete pipeline event can refer to the MSDN documentation:
The image also reflects the three main contributors to asp:
1. HttpModule
2. Httphandlerfactory
3. HttpHandler
Have you ever thought about how many of these three participants are involved in each of these kinds of subjects?
To answer this question clearly, I prepared the following form:
| Pipeline contributor |
Number of participants per request |
| HttpModule |
>= 0 |
| Httphandlerfactory |
1 |
| HttpHandler |
1 |
Why introduce httphandlerfactory? Please see my blog detail httphandler mapping process, today do not repeat this piece of content.
Except for the httphandlerfactory, we can see that in the ASP. HttpHandler, there should be only one, and HttpModule is optional.
In turn, we can not understand that: HttpHandler is the protagonist of the processing request (indispensable), HttpModule is a supporting role (can not)?
Get back to the top to understand HttpApplication
We've been talking about the ASP, so who's in control of the pipeline process?
The answer is: HttpApplication object.
1. HttpApplication subdivides its processing and raises different events at different stages, allowing HttpModule to subscribe to events to be added to the requested process.
2. During the processing of the request, the HttpApplication object plays a key role in the process of control and processing.
3. HttpApplication acquires a IHttpHandler instance at a fixed stage and then passes the requested response process to a specific ihttphandler.
How is HttpApplication produced and how does it work?
1. The HttpApplication object is reused and is created when httpruntime cannot obtain an idle instance from HttpApplicationFactory.
2. HttpRuntime will hand each request to a HttpApplication object for processing.
3. The HttpApplication object is responsible for loading all the HttpModule at initialization time.
4. Each HttpApplication object controls the pipeline process that belongs to it (explained earlier).
HttpApplication is a very important type, and many of its functions are part of the framework's foundation, and we don't need to call it, so we don't use it.
I do not want to let the blog overreaching, below to see the protagonist today.
Get back to the top to understand HttpHandler
Before talking about httpruntime will send the request to HttpApplication to handle, at this time you have not thought of such a question: why HttpApplication not directly processing the request, but to a HttpHandler object to deal with it?
The answer is that the content of each request may be different and there is a multiplicity of them, so that ASP. NET uses the abstract factory pattern to process these requests. Asp. NET in the schema of Web. config, allows us to specify that some requests are mapped to a httphandlerfactory, for example:
<!--for IIS6 configuration--><system.web>
When a request is matched with a rule, ASP. NET calls the GetHandler method of the matching httphandlerfactory to get a HttpHandler instance, and finally a HttpHandler instance to handle the current request .
How does HttpApplication submit the request to the HttpHandler instance for processing?
To understand this process, let's look at the definition of the IHttpHandler interface:
This interface is used for synchronous invocation//asynchronous version of the interface usage please refer to: Http://www.cnblogs.com/fish-li/archive/2011/11/20/2256385.htmlpublic interface ihttphandler{ //Gets a value that indicates whether the IHttpHandler instance can be used by other requests. bool IsReusable {get;} Enable the processing of HTTP WEB requests by implementing a custom HttpHandler for the IHttpHandler interface. void ProcessRequest (HttpContext context);}
HttpApplication is called by the interface (the ProcessRequest method) when a request is handed over to the HttpHandler instance for processing.
Topics related to HttpHandler:
1. Asynchronous HttpHandler: Various asynchronous operations that dwell on ASP.
2. How to reuse HttpHandler: elaborate on the HttpHandler mapping process.
Typical applications of Httphanlder
Usually I go so create a ashx file (Httphanlder) in response to a particular request.
So, we should understand this httphanlder: a httphanlder is used to respond to a specific type of request.
What are the httphanlder that we often use?
What do we usually do with Httphanlder?
Get back to the top to understand HttpModuleThe purpose of design Httphanlder is clear: Generate response results.
So, what is the design HttpModule?
As mentioned earlier, a httphanlder is used to process a particular type of request, and each ASPX, ashx, can be considered a class of requests. Sometimes we find that all pages may require some of the same checking functionality (such as identity checking), and if we can only use Httphanlder, then we have to make all the pages call the same check function. Who is willing to do these repetitive things? Perhaps some people will answer, you can implement a base class, put the check function in the base class to invoke. However, this approach only solves the problem of repeated calls, which can cause the code to lose flexibility (extensibility), just imagine: what if you need to add a new check function, or replace the original check logic with a new check method? You can only modify the base class, right?
The purpose of designing HttpModule is to provide a flexible way to solve this function reuse problem. It takes the design pattern of the event (Observer) and extracts the functions required by some httphanlder to form different observer types that can be compiled into a class library for common use by multiple Web site projects. To make the ASP. NET pipeline more flexible, ASP. NET allows us to freely configure the required HttpModule in Web. config, for example:
<!--configuration for IIS6--><system.web>
The configuration simply tells asp: these HttpModule need to be run up. Have you ever wondered how these httpmodule get into the pipeline? I just said that HttpModule will subscribe to these events, then where are the events subscribed to? Let's take a look at the definition of the IHttpModule interface:
This interface is used for synchronous call//asynchronous usage Please refer to: Http://www.cnblogs.com/fish-li/archive/2011/11/20/2256385.htmlpublic interface IHttpModule { // Initialize the module and make it ready for processing requests. void Init (HttpApplication app); void Dispose ();}
Note that the key Init method, which passes in a parameter of type HttpApplication, has the HttpApplication object, and HttpModule can subscribe to all HttpApplication events. Take a look at the following sample code:
Typical applications of HttpModule
This module is used to set output caching for requests that indicate a cache in the configuration file, and the sample code has been introduced in some ways that the previous blog did not modify the code to optimize the performance of the ASP. In fact, the most fundamental way to set the output cache is to call Response.Cache's public methods to modify the output response header.
What are we going to do with HttpModule?
What requests can HttpModule handle?
Back to the top three main objects summaryBefore I introduced the Httpapplication,httphanlder and HttpModule, here to re-comb the relationship between the three.
During the processing of the request, the HttpApplication object plays a major role in controlling the pipeline process, and it is responsible for advancing the entire processing process, in addition to triggering different events at different stages (for HttpModule use). The HttpApplication object also looks for a suitable ihttpapplicationfactory instance based on the current request, and finally gets a IHttpHandler instance to handle the request.
These three types are designed to:
1. HttpApplication controls the processing process, triggering different events at different stages.
2. Due to the multiplicity of requests, each request is handled by a HttpHandler object.
3. For some common functions, especially those unrelated to the response, the design of HttpModule is the most appropriate.
Back to top case demoQ: I have some HTML files, I need to do the authentication check (judging session), how can I implement?
Take a look at my solution when you're ready:
Q: I need to compress the response results of all the ASP.
Take a look at my solution when you're ready:
Back to top how to choose?Before you end this blog, ask your readers: do you know when to choose HttpHandler or HttpModule now?
If you don't see it yet, I'll tell you at the end. A Recognition method:
1. If you want to respond to a class of requests , select HttpHandler.
2. If you want to modify or check all requests (in short, do not generate response results ), select HttpModule.
Finally, let me leave you with a topic, which of the following ASP. NET features are they implemented by?
1. Session
2. Identity verification
3. URL authorization Check
3. Viewing tracking information through Trace.axd
4. OutputCache
5. Disable the download of config file
6. Disable viewing of downloaded source code files
Note: The topic of this article is: Choose HttpHandler or HttpModule, so please don't pull away.
Transferred from: http://www.cnblogs.com/fish-li/archive/2013/01/04/2844908.html
Choose HttpHandler or HttpModule?