博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

在WebConfig中注册自定义HttpModule的方法

Posted on 2013-02-05 11:16  Object.prototype  阅读(1204)  评论(0编辑  收藏  举报

下面是一段自定义的httpmodule

Source Code
 1 namespace WebLibrary.Modules
 2 {
 3     public class MSNModule : IHttpModule
 4     {
 5         /// <summary>
 6         /// You will need to configure this module in the Web.config file of your
 7         /// web and register it with IIS before being able to use it. For more information
 8         /// see the following link: http://go.microsoft.com/?linkid=8101007
 9         /// </summary>
10         #region IHttpModule Members
11  
12         public void Dispose()
13         {
14             //clean-up code here.
15         }
16  
17         public void Init(HttpApplication app)
18         {
19             app.BeginRequest += app_BeginRequest;
20         }
21  
22         void app_BeginRequest(object sender, EventArgs e)
23         {
24            
25         }
26  
27         #endregion
28     }
29 }

 

下面2种注册方式

  1. 在site目录的webconfig中添加如下结点
    View Code
    <system.web>
     <httpModules>
          <add name="MSNModule" type="WebLibrary.Modules.MSNModule"/>
        </httpModules>
    </system.web>

    格式就是: <add name="<httpmodule类名>" type="<命名空间.httpmodule类名>,<程序集名>"/>

  2. http://www.cnblogs.com/lyk831216/archive/2011/08/01/2124181.htm

注意: 似乎httpmodule不能在子目录下的webconfig中注册,因为site运行时只会加载一次httpmodule(读取site目录的webconfig)