Ihttpmodule Interface Definition: Provides module initialization and event handling for implementation classes. It contains two methods: dispose () and init ();
Custom ihttpmodule Interface
Two steps are required to implement a custom ihttpmodule interface:
1) implement a class that inherits the ihttpmodule Interface
2) register the custom httpmodule in the web. config file.
public class customermodule: ihttpmodule {# region ihttpmodule member public void dispose () {} public void Init (httpapplication context) {context. beginrequest + = new eventhandler (context_beginrequest); context. endrequest + = new eventhandler (context_endrequest);} void context_beginrequest (Object sender, eventargs e) {httpapplication application = (httpapplication) sender; application. context. response. write ("Custom modulerequest start");} void context_endrequest (Object sender, eventargs e) {httpapplication application = (httpapplication) sender; application. context. response. write ("Custom modulerequest ended"); }# endregion}
Web. config
<Httpmodules> <Add name = "customermodule" type = "webapplication3.customermodule, webapplication3"/>
ASP. NET built-in httpmodule
In C: \ WINDOWS \ Microsoft. net \ framework \ v2.0.50727 \ config \ Web. there are many ASP under config. net built-in httpmoudle is loaded by default. If you do not want to load some of them, you can remove them to improve some performance, such:
<Remove name = "windowsauthentication"/>
The built-in functions are as follows:
<Httpmodules> <Add name = "outputcache" type = "system. web. caching. outputcachemodule "/> <Add name =" session "type =" system. web. sessionstate. sessionstatemodule "/> <Add name =" windowsauthentication "type =" system. web. security. windowsauthenticationmodule "/> <Add name =" formsauthentication "type =" system. web. security. formsauthenticationmodule "/> <Add name =" passportauthentication "type =" system. web. security. passportauthenticationmodule "/> <Add name =" rolemanager "type =" system. web. security. rolemanagermodule "/> <Add name =" urlauthorization "type =" system. web. security. urlauthorizationmodule "/> <Add name =" fileauthorization "type =" system. web. security. fileauthorizationmodule "/> <Add name =" anonymousidentification "type =" system. web. security. anonymousidentificationmodule "/> <Add name =" Profile "type =" system. web. profile. profilemodule "/> <Add name =" errorhandlermodule "type =" system. web. mobile. errorhandlermodule, system. web. mobile, version = 2.0.0.0, culture = neutral, publickeytoken = b03f5f7f11d50a3a "/> <Add name =" servicemodel "type =" system. servicemodel. activation. httpmodule, system. servicemodel, version = 3.0.0.0, culture = neutral, publickeytoken = b77a5c561934e089 "/>
Global. asax and httpmodule
In ASP. NET, global. asax is a global file that can register applications.ProgramAnd session events, you can also register the events exposed in httpmodule (including the built-in httpmodule and custom httpmodule), there are two considerations:
1) each time an HTTP request arrives, the application events are all initiated, but application_start and application_end are only triggered when the first resource file is accessed.
2) httpmodule cannot register or respond to session events. session events can only be registered in global. asax.
Public class customermodule: ihttpmodule {// custom exposed event public event eventhandler exposedevent; # region ihttpmodule member public void dispose () {} public void Init (httpapplication context) {context. beginrequest + = new eventhandler (context_beginrequest); context. endrequest + = new eventhandler (context_endrequest);} void context_beginrequest (Object sender, eventargs e) {httpapplication application = (HT Tpapplication) sender; application. context. response. write ("Custom modulerequest start");} void context_endrequest (Object sender, eventargs e) {httpapplication application = (httpapplication) sender; application. context. response. write ("Custom modulerequest ended"); // trigger event onexposedevent (New eventargs () ;}# endregion protected void onexposedevent (eventargs e) {If (exposedevent! = NULL) {exposedevent (this, e );}}}
Add the following in global. asax:Code
Protected void customermodule_exposedevent (Object sender, eventargs e) {response. Write ("from global. asax ");}
The event name in global. asax is defined as module name_event name. The module name is the module name you registered in Web. config.