How does httpmodule work?
When an HTTP request arrives at httpmodule, the entire ASP. the. NET Framework system has not processed this HTTP request. That is to say, httpmodule is the "Only Way" for an HTTP request ", therefore, some required information can be appended to the HTTP request information before the HTTP request is passed to the real request processing center (httphandler, you can also perform some additional work on the intercepted HTTP request information, or simply terminate the HTTP request that meets certain conditions in some cases, so as to act as a filter.
Example 1:
Using system; using system. collections. generic; using system. text; using system. web; namespace myhttpmodule {// <summary> // Description: Used to implement your own httpmodule class. /// Author: wenye // contact: stwyhm@cnblogs.com /// </Summary> public class myfirsthttpmodule: ihttpmodule {private void application_beginrequest (Object sender, eventargs E) {httpapplication application = (httpapplication) sender; httpcontext context = application. context; httprequest request = application. request; httpresponse response = application. response; response. write ("I'm from beginrequest in custom httpmodule <br/>");} private void application_endrequest (Object sender, eventargs e) {httpapplication application = (httpapplication) sender; httpcontext context = application. context; httprequest request = application. request; httpresponse response = application. response; response. write ("I come from the endrequest in the Custom httpmodule <br/>") ;}# region ihttpmodule member public void dispose () {} public void Init (httpapplication application) {application. beginrequest + = new eventhandler (application_beginrequest); application. endrequest + = new eventhandler (application_endrequest) ;}# endregion }}
Perform the following configuration in Web. config:
<add name="MyFirstHttpModule" type="MyHttpModule.MyFirstHttpModule,MyHttpModule"/>
Httpmodule
An HTTP request will be sent to the httphandler container at a certain time point (resolverequestcache event) during the HTTP module container transfer process. After this event, the httpmodule container will create an httphandler entry instance, but does not hand over the HTTP Request control at this time, but will continue to trigger the acquirerequeststate event and prerequesthandlerexcute event. After the prerequesthandlerexcute event, the httpmodule window will temporarily hand over control to the httphandler container for real HTTP request processing.
In the httphandler container, the processrequest method is executed to process HTTP requests. After the container httphandler processes the entire HTTP request, the control is returned to the httpmodule. The httpmodule continues to perform layer-by-layer forwarding actions on the processed HTTP request information stream until it is returned to the client.
Figure 1: httpmodule Lifecycle
Example 2: Verify the lifecycle of the httpmodule
Using system; using system. collections. generic; using system. text; using system. web; namespace myhttpmodule {public class validaterhttpmodule: ihttpmodule {# region ihttpmodule member public void dispose () {} public void Init (httpapplication) {application. beginrequest + = new eventhandler (application_beginrequest); application. endrequest + = new eventhandler (application_endrequest); application. prerequesthandlerexecute + = new eventhandler (application_prerequesthandlerexecute); application. postrequesthandlerexecute + = new eventhandler (application_postrequesthandlerexecute); application. releaserequeststate + = new eventhandler (application_releaserequeststate); application. acquirerequeststate + = new eventhandler (application_acquirerequeststate); application. authenticaterequest + = new eventhandler (application_authenticaterequest); application. authorizerequest + = new eventhandler (application_authorizerequest); application. resolverequestcache + = new eventhandler (application_resolverequestcache); application. presendrequestheaders + = new eventhandler (application_presendrequestheaders); application. presendrequestcontent + = new eventhandler (application_presendrequestcontent);} void application_presendrequestcontent (Object sender, eventargs e) {httpapplication application = (httpapplication) sender; application. context. response. write ("application_presendrequestcontent <br/>");} void application_presendrequestheaders (Object sender, eventargs e) {httpapplication application = (httpapplication) sender; application. context. response. write ("application_presendrequestheaders <br/>");} void application_resolverequestcache (Object sender, eventargs e) {httpapplication application = (httpapplication) sender; application. context. response. write ("application_resolverequestcache <br/>");} void application_authorizerequest (Object sender, eventargs e) {httpapplication application = (httpapplication) sender; application. context. response. write ("application_authorizerequest <br/>");} void application_authenticaterequest (Object sender, eventargs e) {httpapplication application = (httpapplication) sender; application. context. response. write ("application_authenticaterequest <br/>");} void application_acquirerequeststate (Object sender, eventargs e) {httpapplication application = (httpapplication) sender; application. context. response. write ("application_acquirerequeststate <br/>");} void application_releaserequeststate (Object sender, eventargs e) {httpapplication application = (httpapplication) sender; application. context. response. write ("application_releaserequeststate <br/>");} void application_postrequesthandlerexecute (Object sender, eventargs e) {httpapplication application = (httpapplication) sender; application. context. response. write ("application_postrequesthandlerexecute <br/>");} void application_prerequesthandlerexecute (Object sender, eventargs e) {httpapplication application = (httpapplication) sender; application. context. response. write ("application_prerequesthandlerexecute <br/>");} void application_endrequest (Object sender, eventargs e) {httpapplication application = (httpapplication) sender; application. context. response. write ("application_endrequest <br/>");} void application_beginrequest (Object sender, eventargs e) {httpapplication application = (httpapplication) sender; application. context. response. write ("application_beginrequest <br/>") ;}# endregion }}
Operation of multiple custom HTTP modules
From the running results, we can see that the order in which custom httpmodule is introduced in the web. config file determines the order in which multiple custom httpmodules process an HTTP request. Note: by default, the several httpmodules are first loaded by ASP. NET Framework.
Example 3: (code similar example 2)
Terminate this HTTP request in httpmodule
Httpmodule can be used to terminate an HTTP request when a condition is met by calling httpapplication. completerequest.
It should be noted that even if httpapplication. the completerequest () method terminates an HTTP request, Asp. net Framework will still trigger the three events following httpapplication: endrequest event, presendrequestheaders event, and presendrequestcontent event.
If multiple custom httpmodules exist, when module1 terminates an HTTP request, the HTTP request will no longer trigger the corresponding event in module2, however, the last three events of module2 will still be triggered.
Example 4:
Using system; using system. collections. generic; using system. text; using system. web; namespace myhttpmodule {public class completerequesthttpmodule: ihttpmodule {# region ihttpmodule member public void dispose () {} public void Init (httpapplication) {application. beginrequest + = new eventhandler (application_beginrequest);} void application_beginrequest (Object sender, eventargs e) {httpapplic Ation application = (httpapplication) sender; application. completerequest (); application. Context. response. Write ("the request is terminated. ") ;}# Endregion }}
Reproduced in: http://www.cnblogs.com/stwyhm/archive/2006/08/09/471729.html