65.9K
CodeProject 正在变化。 阅读更多。
Home

自定义 HTTP 模块

emptyStarIconemptyStarIconemptyStarIconemptyStarIconemptyStarIcon

0/5 (0投票)

2013 年 10 月 11 日

CPOL

2分钟阅读

viewsIcon

15971

downloadIcon

18

在开始之前,请查看位于 C:\Windows\Microsoft.NET\Framework\versionnumber\CONFIG\Web.config 的 Web.config。 您可以看到

在开始之前,请查看位于
C:\Windows\Microsoft.NET\Framework\versionnumber\CONFIG\Web.config 的 Web.config

您可以看到框架中现有的 HTTPModule

<system.web>
        <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>
 </system.web>


要创建自定义 HTTPModule,我们必须实现 System.Web.IHttpModule 接口。
IHTTPModule 的元数据如下所示:

using System;

namespace System.Web
{
    // 摘要
    //     为实现类提供模块初始化和处置事件。
    public interface IHttpModule
    {
        // 摘要
        //     处置由实现模块使用的资源(内存除外)
        //     System.Web.IHttpModule。
        void Dispose();
        //
        // 摘要
        //     初始化一个模块并准备它来处理请求。
        //
        // 参数
        //   context
        //     一个 System.Web.HttpApplication,它提供对方法、属性的访问,
        //     以及 ASP.NET 应用程序中所有应用程序对象的通用事件
        void Init(HttpApplication context);
    }
}
    
我们可以从创建一个类 HTTPModuleClass 并继承 IHTTPModule 开始。创建一个事件处理程序 public event EventHandler BeginRequest;
正如我们看到的,IHTTPModule 有

    * Dispose()
    * Init(HttpApplication context);

在我们的自定义类 HTTPModuleClass 中实现 Init 并声明 BeginRequest 的事件处理程序,当 ASP.NET 响应请求时,该处理程序将作为 HTTP 管道链中执行的第一个事件发生。
 
public void Init(HttpApplication context)
{
     context.BeginRequest += new EventHandler(OnBeginRequest);
}
 
 然后定义 OnBeginRequest 事件。

public void OnBeginRequest(object sender, EventArgs e)
{
    BeginRequest(this, new EventArgs());
}


 所以现在我们的类看起来像这样:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// HTTPModuleClass 的摘要说明
/// </summary>
namespace HTTPModuleClassTest
{
    public class HTTPModuleClass : IHttpModule
    {
        public event EventHandler BeginRequest;

        public void Dispose()
        {

        }

        public void Init(HttpApplication context)
        {
            context.BeginRequest += new EventHandler(OnBeginRequest);
        }

        public void OnBeginRequest(object sender, EventArgs e)
        {
           BeginRequest(this, new EventArgs());
        }
    }
}

现在我们可以移到 Global.asax.导入命名空间
<%@ Import Namespace="HTTPModuleClassTest" %>
并创建事件 HTTPModuleClass_BeginRequest,如下所示:

void HTTPModuleClass_BeginRequest(object sender, EventArgs e)
{
    HttpBrowserCapabilities httpBrowser = this.Request.Browser;
    Response.Write("您正在使用 " + httpBrowser.Browser);
    Response.Write(" [版本: " + httpBrowser.Version + "]");       
}

最后进入配置部分。打开 web.config 并添加新的自定义 HTTPModule。

<httpModules>
<add name="HTTPModuleClass" type="HTTPModuleClassTest.HTTPModuleClass"/>
</httpModules>

构建并运行应用程序。

 下载示例应用程序.

© . All rights reserved.