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

自定义IHttpModule接口

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

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

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

 public class CustomerModule:IHttpModule
    {
        #region IHttpModule 成员

        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("自定义ModuleRequest开始");
        }
        void context_EndRequest(object sender, EventArgs e)
        {
            HttpApplication application = (HttpApplication)sender;

            application.Context.Response.Write("自定义ModuleRequest结束");
        }
        #endregion
    }

 

web.config

      <httpModules>
        <add name="customerModule" type="WebApplication3.CustomerModule, WebApplication3"/>
      </httpModules>
ASP.NET 内置的HttpModule
在C:\Windows\Microsoft.NET\Framework\v2.0.50727\CONFIG\web.config下有很多ASP.NET内置的HttpMoudle默认是加载的如果你不想加载其中的一些可以Remove掉这样可以提高一些性能如:
<remove name="WindowsAuthentication"/>
内置的如下:
        <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"/>
        </httpModules>
Global.asax与HttpModule
在ASP.NET中Global.asax是一个全局文件,他可以注册应用程序和Session事件,还可以注册HttpModule中暴露的事件(包括内置的HttpModule和自定义的HttpModule),有2点注意事项:
1)每当一个Http Request到达时,应用程序事件都要出发遍,但是Application_Start和Application_End仅在第一个资源文件被访问时触发。
2)HttpModule无法注册和响应Session 事件,Session事件只能在Global.asax中注册
    public class CustomerModule:IHttpModule
    {
        //自定义暴露的事件
        public event EventHandler ExposedEvent;
      
        #region IHttpModule 成员

        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("自定义ModuleRequest开始");
        }
        void context_EndRequest(object sender, EventArgs e)
        {
            HttpApplication application = (HttpApplication)sender;

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

            //触发事件
            OnExposedEvent(new EventArgs());
        }
        #endregion

        
        protected  void OnExposedEvent( EventArgs e)
        {
            if (ExposedEvent!=null)
            {
                ExposedEvent(this, e);
            }
        }
    }

 

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

        protected void customerModule_ExposedEvent(object sender, EventArgs e)
        {
            Response.Write("来自Global.asax");
        }

 

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

posted on 2010-03-20 22:09  tedyding  阅读(1265)  评论(0编辑  收藏  举报