Remember: When httphandler performs image anti-theft and other applications, you must set related configurations on IIS.
If you use vs2008 for local debugging.
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;
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 following namespace is referenced at the beginning of the program:
Using system;
Using system. Web;
Because classes such as httpapplication, httpcontext, and httpresponse are defined in system. Web, system. Web namespaces must be referenced.
The mymodule class implements the ihttpmodule interface. The init method specifies the method for implementing the beginrequest and endrequest events. In the two methods, some information is simply printed separately.
Next, register this class in the web. config file and you can use this httpmodule. The registration method is as follows:
<Configuration>
<System. Web>
<Httpmodules>
<Add name = "mymodule" type = "mymodule, mymodule"/>
</Httpmodules>
</System. Web>
</Configuration>
Now let's take a look at the effect. Compile An ASPX page test. aspx with the following content:
<%
Response. Write ("%>
After running:
In-depth research on httpmodule
Httpmodule processes a series of events of the httpapplication object to influence the HTTP processing pipeline. These events are registered in the init method of httpmodule, including:
Beginrequest
Authenticaterequest
Authorizerequest
Resolverequestcache
Acquirerequeststate
Prerequesthandlerexecute
Postrequesthandlerexecute
Releaserequeststate
Updaterequestcache
Endrequest
Some of the events correspond to events in global. asax. The corresponding relationship is as follows:
Httpmodule global. asax
Beginrequest application_beginrequest
Authenticaterequest application_authenticaterequest
Endrequest application_endrequest
In example 1, The beginrequest and endrequest events are processed. The processing methods for other events are similar.
According to httphandler, some of these events occur before httphandler, and some occur after httphandler completes processing. It is very important to understand the sequence of events, because the objects on the server have different performance in different time periods. One example is the use of session. Not all events can process sessions, but only a limited number of events. For detailed procedures, refer to the following HTTP request processing lifecycle diagram.
Use httpmodule to implement the Permission System
This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/JOHNCOOLS/archive/2006/03/06/616898.aspx