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 ("%>