In-depth analysis of HttpHandler and HttpModule in Asp. NET

Source: Internet
Author: User


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.

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.