In our. NET web. config configuration file, we often see HttpHandler and HttpModule. Do you really understand these two words? The following describes the functions and applications of HttpHandler and HttpModule.
What is IHttpHandler?
IHttpHandler defines some basic conventions for implementing HTTP requests. A simple understanding is to configure an HttpHandler to implement a URL request. If an IHttpHandler defines its implementation class, it is equivalent to overwriting.
IHttpHandler usage
HttpHandler, you need to inherit from the IHttpHandler interface, the following code:
Public class SampleHttpHandler: IHttpHandler
{
Public void ProcessRequest (HttpContext context)
{
Context. Response. ContentType = "text/plain ";
Context. Response. Write ("result returned by SampleHttpHandler! ");
}
}
SampleHttpHandler inherits from the IHttpHandler interface. The interface has a ProcessRequest method to be implemented. This method is the specific content to be processed.
After the cs code is written, you only need to configure web. config, as shown below:
<Add verb = "*" path = "/SampleHttpHandler" type = "Sample. SampleHttpHandler"/>
In this way, SampleHttpHandler is configured successfully, and "/SampleHttpHandler" is the accessible path.
What is HttpModule?
HttpModule is a filter. Any HTTP request must be processed by the HttpModule before any processing. The working principle is to listen to HttpRequest and perform unified request processing. For example, you can process and redirect all the URLs ending with "/" to the URLs ending.
HttpModule usage
HttpModule must implement the IHttpModule interface and perform specific processing in the implementation class.
For example:
<HttpModules>
<Add name = "SampleHttpModule" type = "Sample. SampleHttpModule, Sample"/>
</HttpModules>
You only need to correctly configure the full space path of SampleHttpModule.
The following figure shows the relationship between HttpHandler and HttpModule.
At this point, you should understand the differences between HttpHandler and HttpModule, and when to use HttpHandler and HttpModule. Comparing HttpHandler to a complete process, HttpModule is responsible for a process.