Asp.Net IHttpHandler introduced ASP.NET two commonly used processing interfaces when responding to Http requests are IHttpHandler and IHttpModule.
In general, IHttpHandler is used to handle a specific type of request, such as the separate processing of each *.asp, *.aspx file. IHttpModule is usually used to handle operations that are commonly required for the request, such as performing some of the same checks on the request page.
Let's take a look at the processing steps of the IIS server when making the corresponding HTTP request. ASP.NET has the concept of pipeline, which means that each ASP.NET request will have a series of corresponding operations in IIS to form a line-like sequence.
ASP.NET pipeline introduction
Let's take a look at the processing timing diagram of the pipeline:
As can be seen from the figure, after the request arrives, the implementation is processed by HttpModule and then the ProcessRequest() method of HttpHandler is called to make the specific response. Therefore, it is not difficult to see why the handling of class-specific requests is placed in the HttpHandler class while doing some checks that are common to all requests in the HttpModule.
Code practice
IHttpHandler
The author recently came across the use of IHttpHandler to handle client interface calls in the project, so let's briefly discuss the simple interface design based on IHttpHandler.
IHttpHandler interface has only two members:
The IsReusable attribute identifies whether the HttpHandler object can be used by other instances, and we usually set it to True. The ProcessRequest() method is a specific response to the request, and we only need to put the specific business logic operation here.
First, create a new web project and add a Handler class:
The RayHandler class implements the ProcessRequest() function of the IHttpHandler interface, which is just a direct output of a piece of text.
Then we need to add the following configuration in the Web.config file:
path indicates URL matching, such as *.ray, which means that the Handler will respond to URL requests ending in ".ray", verb indicates the request method, such as Get/Post, and * means that it matches all. type indicates the type of Handler class, WebApplication2.RayHandler is the class name, WebApplication2 refers to the name of the assembly in the Bin directory, for example, the assembly name in the example is WebApplication2.dll, and there is no need to define a suffix name here.
Start the site, enter the URL ending in ".ray", and you can see the following result:
IHttpHandlerFactory Overview
Sometimes we may need to deal with multiple different suffixes, one suffix corresponding to a Handler class, and this is what our Web.config file looks like:
If we have a lot of HttpHandler implementation classes, then our Web.config file configuration is bound to look verbose. Or in some cases, when we can only determine which Handler responds when the program is running, we need to use IHttpHandlerFactory.
IHttpHandlerFactory is defined as follows:
Among them:
GetHandler(): Returns an instance that implements the IHttpHandler interface; ReleaseHandler(): Enables Factory to reuse an existing Handler instance. Take the above ray and rss requests as an example, implement the Factory class:
In this case, the configuration in Web.config is as follows:
At this time, the function of using the Factory class to correspond to different specific handlers is implemented, simplifying the configuration.
Scalable IHttpHandlerFactory
In the above implementation, if the program needs to add a new suffix handling method in the future, it needs to modify the Switch statement in GetHandler(), which may also cause errors or bring other security risks. So, is it possible to keep the HandlerFactory class unchanged for subsequent extensions?
The answer is definitely yes. Readers who are familiar with the design pattern should understand that this is a simple factory pattern, and to achieve the previous functions, we can use the design mode called advanced points.
And here, we can also use the language feature of the C# language - reflection. Through the reflection mechanism of C#, we reflect the corresponding Hanlder type according to the suffix of the URL, as long as we agree on the correspondence between the suffix name of the URL and the class name of the Handler.
For example, we rewrite GetHandler() as follows:
In this case, you only need to put the Handler class in the method under the same namespace as the HandlerFactory class and configure it correctly in Web.config. For example, if there is a RayHandler class, then the following configuration should be added to automatically match:
summary This article briefly introduces the use of IHttpHandler in ASP.NET, provides the implementation of IHttpHandlerFactory in the processing of multiple Handler requests, and finally, improves a scalable multi-request Handler implementation using the reflection mechanism of C#.
|