IHttpModule接口的定义:向实现类提供模块初始化和处置事件。它包含2个方法:Dispose()和Init();

自定义IHttpModule接口
实现一个自定义的IHttpModule接口需要2个步骤:

1)实现一个继承了IHttpModule接口的类

2)在Web.config文件中注册这个自定义HttpModule

view source

print?01 public class CustomerModule:IHttpModule

02    {

03        #region IHttpModule 成员

04        public void Dispose()

05        {

06              

07        }

08        public void Init(HttpApplication context)

09        {

10            context.BeginRequest += new EventHandler(context_BeginRequest);

11            context.EndRequest += new EventHandler(context_EndRequest);

12        }

13        void context_BeginRequest(object sender, EventArgs e)

14        {

15            HttpApplication application = (HttpApplication)sender;

16            application.Context.Response.Write("自定义ModuleRequest开始");

17        }

18        void context_EndRequest(object sender, EventArgs e)

19        {

20            HttpApplication application = (HttpApplication)sender;

21            application.Context.Response.Write("自定义ModuleRequest结束");

22        }

23        #endregion

24    }

 

web.config

view source

print?1 <httpModules>

2   <add name="customerModule" type="WebApplication3.CustomerModule, WebApplication3"/>

3 </httpModules>

ASP.NET 内置的HttpModule
view source

print?1 <FONT color=#7b7d62>在C:\Windows\Microsoft.NET\Framework\v2.0.50727\CONFIG\web.config下有很多ASP.NET内置的HttpMoudle默认是加载的如果你不想加载其中的一些可以Remove掉这样可以提高一些性能如:</FONT>

view source

print?1 <remove name="WindowsAuthentication"/>

view source

print?1 内置的如下:

view source

print?01 <httpModules>

02     <add name="OutputCache" type="System.Web.Caching.OutputCacheModule"/>

03     <add name="Session" type="System.Web.SessionState.SessionStateModule"/>

04     <add name="WindowsAuthentication" type="System.Web.Security.WindowsAuthenticationModule"/>

05     <add name="FormsAuthentication" type="System.Web.Security.FormsAuthenticationModule"/>

06     <add name="PassportAuthentication" type="System.Web.Security.PassportAuthenticationModule"/>

07     <add name="RoleManager" type="System.Web.Security.RoleManagerModule"/>

08     <add name="UrlAuthorization" type="System.Web.Security.UrlAuthorizationModule"/>

09     <add name="FileAuthorization" type="System.Web.Security.FileAuthorizationModule"/>

10     <add name="AnonymousIdentification" type="System.Web.Security.AnonymousIdentificationModule"/>

11     <add name="Profile" type="System.Web.Profile.ProfileModule"/>

12     <add name="ErrorHandlerModule" type="System.Web.Mobile.ErrorHandlerModule, System.Web.Mobile, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>

13     <add name="ServiceModel" type="System.ServiceModel.Activation.HttpModule, System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>

14 </httpModules>

Global.asax与HttpModule
view source

print?1 <FONT color=#7b7d62>在ASP.NET中Global.asax是一个全局文件,他可以注册应用程序和Session事件,还可以注册HttpModule中暴露的事件(包括内置的HttpModule和自定义的HttpModule),有2点注意事项:</FONT>

view source

print?1 <FONT color=#7b7d62>1)每当一个Http Request到达时,应用程序事件都要出发遍,但是Application_Start和Application_End仅在第一个资源文件被访问时触发。</FONT>

view source

print?1 <FONT color=#7b7d62>2)HttpModule无法注册和响应Session 事件,Session事件只能在Global.asax中注册</FONT>

view source

print?01 public class CustomerModule:IHttpModule

02 {
03 //自定义暴露的事件
04 public event EventHandler ExposedEvent;
05
06 #region IHttpModule 成员
07 public void Dispose()
08 {
09
10 }
11 public void Init(HttpApplication context)
12 {
13 context.BeginRequest += new EventHandler(context_BeginRequest);
14 context.EndRequest += new EventHandler(context_EndRequest);
15 }
16 void context_BeginRequest(object sender, EventArgs e)
17 {
18 HttpApplication application = (HttpApplication)sender;
19 application.Context.Response.Write("自定义ModuleRequest开始");
20 }
21 void context_EndRequest(object sender, EventArgs e)
22 {
23 HttpApplication application = (HttpApplication)sender;
24 application.Context.Response.Write("自定义ModuleRequest结束");
25 //触发事件
26 OnExposedEvent(new EventArgs());
27 }
28 #endregion
29
30 protected void OnExposedEvent( EventArgs e)
31 {
32 if (ExposedEvent!=null)
33 {
34 ExposedEvent(this, e);
35 }
36 }
37 }

在Global.asax中添加这样的代码

view source
print?1 protected void customerModule_ExposedEvent(object sender, EventArgs e)
2 {
3 Response.Write("来自Global.asax");
4 }

其中Global.asax中事件名的定义是:模块名_事件名。其中模块名是你在Web.config中注册的模块名称

作者:沐雪 文章均系作者原创或翻译,如有错误不妥之处,欢迎各位批评指正。本文版权归作者有,如需转载恳请注明。
​​​ 为之网-热爱软件编程 http://www.weizhi.cc/​