IHttpModule在webconfig中的注册

在asp.net中,提供了两种方式用来解决获取由asp.net服务器创建和维护的HttpApplication对象,方便注册HttpApplication对象的事件处理。这两种方式为:IHtpModule和global.asax方式。这两种方式相同点核心都是IHttpModule接口。

通过IHttpModule接口创建HttpApplication的事件处理程序

该接口在命名空间System.Web下,专门用来定义HttpApplication对象的事件处理。

实现该接口的类称为HttpModule,在asp.net中,该接口的定义如下:

#region Assembly System.Web.dll, v4.0.0.0
// C:Program Files (x86)Reference AssembliesMicrosoftFramework.NETFrameworkv4.5System.Web.dll
#endregion

using System;

namespace System.Web
{
    // Summary:
    //     Provides module initialization and disposal events to the implementing class.
    public interface IHttpModule
    {
        // Summary:
        //     Disposes of the resources (other than memory) used by the module that implements
        //     System.Web.IHttpModule.
        void Dispose();
        //
        // Summary:
        //     Initializes a module and prepares it to handle requests.
        //
        // Parameters:
        //   context:
        //     An System.Web.HttpApplication that provides access to the methods, properties,
        //     and events common to all application objects within an ASP.NET application
        void Init(HttpApplication context);
    }
}

Dispose方法,用来回收Module所使用的非托管资源,如果没有,直接返回即可。

Init方法,这个方法有一个HttpApplication类型的参数,在asp.net中,每当创建一个HttpApplication对象实例,将遍历注册的HttpModule类型,通过反射,依次创建每个注册的HttpModule类型的实例对象,并将这个HttpApplication实例通过Init方法传递给各个HttpModule,这样HttpModule就可以在第一时间完成对HttpApplication的事件注册。

    public class MyHttpModule : IHttpModule
    {
        public void Dispose()
        {
            throw new NotImplementedException();
        }

        public void Init(HttpApplication context)
        {
            context.PostAuthenticateRequest += context_PostAuthenticateRequest;
        }

        void context_PostAuthenticateRequest(object sender, EventArgs e)
        {
            throw new NotImplementedException();
        }
    }

在asp.net中,实现了接口IHttpModule接口只是实现了HttpModule的第一步,如果要让其起作用,我们还需要,将其注册在网站的配置文件中。

说到配置文件,我们必须说说在.net中,网站配置文件的执行顺序,也可以说是级别。

在.Net的系统文件夹中,有针对服务器所有.Net程序的配置文件,配置文件所在的文件夹位于操作系统的:C:WindowsMicrosoft.NETFrameworkv4.0.30319Config目录下,当然不同的.net版本对应的也不同。

在这个文件夹下,有两个重要的配置文件:machine.config和web.config。

machine.config配置文件中保存有针对服务器所有.net程序的基本配置参数。web.config配置文件中保存针对针对此服务器所有web应用程序的基本配置参数。在我们开发的网站项目中的web.config中所做的配置,是专门针对这个网站应用程序的配置文件,在网站因公程序中起作用的配置参数来自这三个配置文件的整合。

在asp.net的网站配置文件web.config中,system.web配置元素的子元素httpModules用来配置网站所使用的HttpModule:httpModules的子元素add用来增加一个新的HttpModule,clear将清楚前面注册的所有HttpModule。

add元素有两个必选的属性name和type。

name:表示这个HttpModule在程序中的名字,在网站应用程序中,可以通过这个名字来找到这个HttpModule对象的引用。HttpApplication的Modules属性表示这个对象所关联的所有HttpModule对象,通过这个name作为索引器,可以找到对应的HttpModule对象。

type:表示HttpModule对象的类型名,asp.net网站可以使用这个类型名,通过反射来动态创建HttpModule对象,类型的写法就是反射中要求的类型名称写法,如果这个类定义在网站中,那么就是一个包含命名空间的类的全名。否则的话,在全名的后面使用逗号翻个,还需要跟上类型所在的程序集的名称,这个程序集的名称不需要包含.dll扩展名。

例如,我们有一个专门的Module类库UserModule,对应的程序集的名称为UserModule.dll,该程序集中有一个处理在线用户的OnlineUserModule,那么我们在配置文件中注册就应该是这样子的。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace UserModule
{
    public class OnlineUserModule:IHttpModule
    {
        public void Dispose()
        {
            throw new NotImplementedException();
        }

        public void Init(HttpApplication context)
        {
            throw new NotImplementedException();
        }
    }
}
    <httpModules>
      <add name="online" type="UserModule.OnlineUserModule"/>
    </httpModules>

对于IIS7.0来说,需要在配置文件的system.webServer配置节点中注册HttpModule。注意此时的配置元素名称为modules。在iis7.0中,可以为MapRequestHandler,LogRequest和PostLogRequest事件添加处理程序。只有在iis7.0集成模式下运行并且与.NET framework3.0或者更高版本一起运行的应用程序,才可以支持这些事件。

  <system.webServer>
    <modules>
      <add name="online" type="UserModule.OnlineUserModule"/>
    </modules>
  </system.webServer>

拓展代码块也可以在iis7.0配置存储区(ApplicationHost.config)的modules元素中注册。在ApplicationHost.config文件中注册的模块具有全局范围,因为他们为所有由IIS7.0承载的web应用程序注册。同样,在ApplicationHost.config文件的globalModules元素中定义的本机代码模块,也具有全局范围。如果web应用不需要全局模块,则可以将其禁用。

原文地址:https://www.cnblogs.com/wolf-sun/p/5244519.html