演练:创建和注册自定义 HTTP 模块

更新:2007 年 11 月

本演练演示自定义 HTTP 模块的基本功能。对于每个请求,都需要调用 HTTP 模块以响应 BeginRequestEndRequest 事件。因此,该模块在处理请求之前和之后运行。

如果 ASP.NET 应用程序是在 IIS 6.0 下运行,则可以使用 HTTP 模块自定义针对 ASP.NET 所提供的资源的请求。这些资源包括 ASP.NET Web 页(.aspx 文件)、Web 服务(.asmx 文件)、ASP.NET 处理程序(.ashx 文件)和您已经映射到 ASP.NET 的任何文件类型。如果 ASP.NET 应用程序是在 IIS 7.0 下运行,则可以使用 HTTP 模块自定义针对 IIS 所提供的任何资源的请求。这不仅包括 ASP.NET 资源,还包括 HTML 文件(.htm 或 .html 文件)、图形文件等等。有关更多信息,请参见IIS 5.0 和 6.0 的 ASP.NET 应用程序生命周期概述IIS 7.0 的 ASP.NET 应用程序生命周期概述

本主题中的示例模块在任何 HTTP 请求的开始阶段都向所请求的 ASP.NET 网页中添加一个消息。在页面得到处理之后,它将添加另外一个消息。该模块还包含相应的代码,以确保它不会向针对任何其他文件类型的请求中添加文本。

每个事件处理程序都编写为该模块的私有方法。在引发已注册的事件时,ASP.NET 将调用该模块中适当的处理程序,以便将信息写入 ASP.NET 网页。

本演练阐释以下任务:

  • 如何创建 HTTP 模块的代码。

  • 如何在 Web.config 文件中注册该模块。

系统必备

若要完成本演练,您需要:

  • Visual Studio 或 Visual Web Developer。

本演练还假定您使用的是 IIS 6.0 或 IIS 7.0。但是,即使您运行的是 ASP.NET Development Server,您也可以看到该模块的功能。

创建自定义 HTTP 模块类

首先,您需要创建一个类文件以实现该模块。

创建自定义 HTTP 模块类

  1. 创建一个 ASP.NET 网站并将它命名为 Handler。

    说明:

    可以为网站选择任何名称。

  2. 如果网站还没有 App_Code 文件夹,请在该站点的根目录下创建一个这样的文件夹。

  3. 在 App_Code 目录中,创建一个名为 HelloWorldModule.vb(对于 Visual Basic)或名为 HelloWorldModule.cs(对于 C#)的类文件。

    说明:

    另外,如果您使用的是 Visual Studio(而不是 Visual Web Developer 速成版),则可以将 HelloWorldModule 创建为一个类库项目,对它进行编译,并将生成的程序集放到 Web 应用程序的 Bin 目录中。

  4. 将下面的代码添加到类文件中:

    Imports Microsoft.VisualBasic
    Imports System.Web
    
    Public Class HelloWorldModule
        Implements IHttpModule
    
        Public Sub New()
        End Sub
    
        Public ReadOnly Property ModuleName() As String
            Get
                Return "HelloWorldModule"
            End Get
        End Property
    
        ' In the Init function, register for HttpApplication 
        ' events by adding your handlers. 
        Public Sub Init(ByVal application As HttpApplication) _
                Implements IHttpModule.Init
            AddHandler application.BeginRequest, _
                AddressOf Me.Application_BeginRequest
            AddHandler application.EndRequest, _
                AddressOf Me.Application_EndRequest
        End Sub
    
        Private Sub Application_BeginRequest(ByVal source As Object, _
                 ByVal e As EventArgs)
            ' Create HttpApplication and HttpContext objects to access 
            ' request and response properties. 
            Dim application As HttpApplication = DirectCast(source, _
                HttpApplication)
            Dim context As HttpContext = application.Context
            Dim filePath As String = context.Request.FilePath
            Dim fileExtension As String = _
                VirtualPathUtility.GetExtension(filePath)
            If fileExtension.Equals(".aspx") Then
                context.Response.Write("<h1><font color=red>" & _
                    "HelloWorldModule: Beginning of Request" & _
                    "</font></h1><hr>")
            End If
        End Sub
    
        Private Sub Application_EndRequest(ByVal source As Object, _
            ByVal e As EventArgs)
            Dim application As HttpApplication = DirectCast(source, _
                HttpApplication)
            Dim context As HttpContext = application.Context
            Dim filePath As String = context.Request.FilePath
            Dim fileExtension As String = _
                VirtualPathUtility.GetExtension(filePath)
            If fileExtension.Equals(".aspx") Then
                context.Response.Write("<hr><h1><font color=red>" & _
                    "HelloWorldModule: End of Request</font></h1>")
            End If
        End Sub
    
        Public Sub Dispose() Implements System.Web.IHttpModule.Dispose
        End Sub
    
    End Class
    
    using System;
    using System.Web;
    public class HelloWorldModule : IHttpModule
    {
        public HelloWorldModule()
        {
        }
    
        public String ModuleName
        {
            get { return "HelloWorldModule"; }
        }
    
        // In the Init function, register for HttpApplication 
        // events by adding your handlers.
        public void Init(HttpApplication application)
        {
            application.BeginRequest += 
                (new EventHandler(this.Application_BeginRequest));
            application.EndRequest += 
                (new EventHandler(this.Application_EndRequest));
        }
    
        private void Application_BeginRequest(Object source, 
             EventArgs e)
        {
        // Create HttpApplication and HttpContext objects to access
        // request and response properties.
            HttpApplication application = (HttpApplication)source;
            HttpContext context = application.Context;
            string filePath = context.Request.FilePath;
            string fileExtension = 
                VirtualPathUtility.GetExtension(filePath);
            if (fileExtension.Equals(".aspx"))
            {
                context.Response.Write("<h1><font color=red>" +
                    "HelloWorldModule: Beginning of Request" +
                    "</font></h1><hr>");
            }
        }
    
        private void Application_EndRequest(Object source, EventArgs e)
        {
            HttpApplication application = (HttpApplication)source;
            HttpContext context = application.Context;
            string filePath = context.Request.FilePath;
            string fileExtension = 
                VirtualPathUtility.GetExtension(filePath);
            if (fileExtension.Equals(".aspx"))
            {
                context.Response.Write("<hr><h1><font color=red>" +
                    "HelloWorldModule: End of Request</font></h1>");
            }
        }
    
        public void Dispose() { }
    }
    
  5. 保存并关闭类文件。

  6. 在“生成”菜单上单击“生成网站”。

    如果网站未生成,请更正存在的任何问题。必须对自定义 HTTP 模块进行编译,否则无法注册该模块。

在 IIS 6.0 和 IIS 7.0 经典模式中注册 HTTP 模块

创建 HelloWorldModule 类之后,可以通过在 Web.config 文件中创建一个项来注册该模块。通过注册 HTTP 模块,可使其能够订阅请求管道通知。

在 IIS 7.0 中,应用程序可以在经典模式或集成模式下运行。在经典模式下,请求的处理方式与在 IIS 6.0 中基本相同。在集成模式下,IIS 7.0 使用管道(管道使其可以与 ASP.NET 共享请求、模块和其他功能)来管理请求。

在 IIS 7.0 经典模式和 IIS 7.0 集成模式下,注册模块的过程有所不同。本节描述与 IIS 6.0 和 IIS 7.0 经典模式相对应的过程。下一节将描述用于注册在 IIS 7.0 集成模式下运行的模块的过程。

为 IIS 6.0 和在经典模式下运行的 IIS 7.0 注册模块

  1. 如果网站还没有 Web.config 文件,请在该站点的根目录下创建一个这样的文件。

  2. 将下面突出显示的代码添加到该 Web.config 文件中:

    <configuration>
      <system.web>
        <httpModules>      <add name="HelloWorldModule" type="HelloWorldModule"/>     </httpModules>
      </system.web>
    </configuration>
    

    这段代码用 HelloWorldModule 的类名和模块名注册该模块。

在 IIS7.0 集成模式下注册 HTTP 模块

在 IIS 7.0 集成模式下注册模块的过程与 IIS 7.0 经典模式下的注册过程稍有不同。

为在集成模式下运行的 IIS7.0 注册模块

  1. 如果网站还没有 Web.config 文件,请在该站点的根目录下创建一个这样的文件。

  2. 将下面突出显示的代码添加到该 Web.config 文件中:

    <configuration>
      <system.webServer>    <modules>      <add name="HelloWorldModule" type="HelloWorldModule"/>    </modules>  </system.webServer>
    </configuration>
    
    说明:

    您还可以使用 IIS 管理器注册模块。有关更多信息,请参见 Configuring Modules in IIS 7.0(在 IIS 7.0 中配置模块)。

    这段代码用 HelloWorldModule 的类名和模块名注册该模块。

测试自定义 HTTP 模块

创建并注册自定义 HTTP 模块后,可以对它进行测试。

测试自定义 HTTP 模块

  1. 在应用程序中添加一个新的 ASP.NET 页面。

  2. 右击刚添加的页,选择“在浏览器中查看”。

    HTTP 模块会将一个字符串追加到响应的开头和末尾。在对其扩展名已分配给 ASP.NET 的文件进行任何请求的过程中,该模块都将自动运行。有关更多信息,请参见 HTTP 处理程序和 HTTP 模块概述

请参见

概念

IIS 5.0 和 6.0 的 ASP.NET 应用程序生命周期概述