Research on httpmodule in ASP. NET

Source: Internet
Author: User
Httphandler implements functions similar to ISAPI extention. It processes request information and sends response ). The httphandler function is implemented through the ihttphandler interface. Httpmodule implements functions similar to ISAPI filter.

Httpmodule implementation

Httpmodules implements functions similar to ISAPI filter. Generally, the following steps are required during development:

1. Write a class to implement the ihttpmodule Interface

2. Implement the init method and register the required Method

3. Registration Method

4. Implement the dispose method. If you need to manually clear the class, you can add the implementation of the dispose method, but this is not necessary. Generally, you can not add any code for the dispose method.

5. register the class you have written in the web. config file.

The following is an example of httpmodules. In this example, only the beginrequest and endrequest events of httpapplication are registered, and relevant information is printed out through the implementation of these events.

// Example 1:
Using system;
Using system. Web; // The namespace that must be referenced
Namespace mymodule
{
Public class mymodule: ihttpmodule
{
Public void Init (httpapplication Application)
{
Application. beginrequest + = (New eventhandler (this. application_beginrequest ));
Application. endrequest + = (New eventhandler (this. application_endrequest ));
}
Private void application_beginrequest (Object source, eventargs E)
{
Httpapplication application = (httpapplication) source;
Httpresponse response = application. Context. response;
Response. Write ("}
Private void application_endrequest (Object source, eventargs E)
{
Httpapplication application = (httpapplication) source;
Httpresponse response = application. Context. response;
Response. Write ("}
Public void dispose ()
{
}
}
}

The mymodule class implements the ihttpmodule interface. The init method specifies the method for implementing the beginrequest and endrequest events.

Next, register this class in the web. config file and you can use this httpmodule. The registration method is as follows:

<configuration>      <system.web>          

Now let's take a look at the effect. Compile An ASPX page test. aspx with the following content:
<%
Response. Write ("%>

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.