共用方式為


逐步解說:建立和註冊自訂 HTTP 模組

更新:2007 年 11 月

本逐步解說說明自訂 HTTP 模組的基本功能。在回應 BeginRequestEndRequest 事件時,每一個要求都會呼叫 HTTP 模組。結果,模組在處理要求之前和之後都會執行。

如果 ASP.NET 應用程式是在 IIS 6.0 下執行,您可以使用 HTTP 模組為 ASP.NET 所服務的資源自訂要求。包括了 ASP.NET 網頁 (.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 網頁中。頁面處理過後會再加入另一個訊息。這個模組所包含的程式碼,可確保不會將文字加入至任何其他檔案類型的要求。

每一個事件處理常式都會撰寫成模組的私用方法。當引發註冊事件 (Registered Event) 時,ASP.NET 會呼叫模組中適當的處理常式,將資訊寫入 ASP.NET 網頁。

本逐步解說所說明的工作包括下列各項:

  • 如何建立 HTTP 模組的程式碼。

  • 如何在 Web.config 檔案中登錄模組。

必要條件

若要完成這個逐步解說,您必須要有:

  • Visual Studio 或 Visual Web Developer。

  • ASP.NET 網站。

本逐步解說也會假設您使用的是 IIS 6.0 或 IIS 7.0。但是,即使您執行 ASP.NET 程式開發伺服器,還是可以看到模組的功能。

建立自訂 HTTP 模組類別

一開始,您要建立一個會實作模組的類別檔。

若要建立自訂 HTTP 模組類別

  1. 如果網站還沒有 App_Code 資料夾,請在網站的根目錄下建立一個。

  2. 在 App_Code 目錄中建立名為 HelloWorldModule.vb (Visual Basic) 或 HelloWorldModule.cs (C#) 的類別檔案。

    注意事項:

    或者,您可以建立 HelloWorldModule 做為類別庫專案、編譯此專案,然後將產生的組件放置在 Web 應用程式的 Bin 目錄中。

  3. 將下列程式碼加入至類別檔案:

    Imports Microsoft.VisualBasic
    Public Class HelloWorldModule
        Implements IHttpModule
    
        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 = CType(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 = CType(source, _
                HttpApplication)
                Dim context As HttpContext = application.Context
            Dim filenameExtension As String = _
                System.IO.Path.GetExtension( _
                HttpContext.Current.Request.FilePath)
            If filenameExtension = ".aspx" Then
            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 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()
        {
        }
    }
    
  4. 儲存並關閉類別檔案。

  5. 按一下 [建置] 功能表中的 [建置網站]。

    如果沒有建置網站,請修正錯誤。自訂 HTTP 模組必須編譯,否則您就無法登錄模組。

在 IIS 6.0 和 IIS 7.0 傳統模式下登錄 HTTP 模組

HelloWorldModule 類別建立完成後,請在 Web.config 檔案中建立一個項目以登錄模組。登錄 HTTP 模組可讓模組訂閱要求管線通知。

在 IIS 7.0 中,應用程式能夠以傳統 (Classic) 或整合 (Integrated) 模式執行。在傳統模式下,處理要求的方式基本上和在 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) 的模組。

在 IIS 7.0 整合模式下登錄 HTTP 模組

在 IIS 7.0 整合模式中登錄模組的程序,和 IIS 7.0 傳統模式的程序稍有不同。

若要登錄在整合模式下執行之 IIS 7.0 的模組

  1. 如果網站還沒有 Web.config 檔案,請在網站的根目錄下建立一個。

  2. 加入下列以粗體顯示的程式碼至 Web.config 檔:

    <configuration>
      <system.webServer>    <modules>      <add name="HelloWorldModule" type="HelloWorldModule"/>    </modules>  </system.webServer>
    </configuration>
    
    注意事項:

    您也可以使用 IIS 管理員登錄模組。如需詳細資訊,請參閱在 IIS 7.0 中設定模組 (英文)。

    程式碼會註冊具有類別名稱和模組名稱 (HelloWorldModule) 的模組。

測試自訂 HTTP 模組

已經建立和登錄自訂 HTTP 模組之後,您可以對其進行測試。

若要測試自訂 HTTP 模組

  1. 在應用程式中加入 ASP.NET 頁面。

  2. 在瀏覽器中要求頁面。

    HTTP 模組會將字串附加至回應的開頭和結尾。在要求其副檔名已指派至 ASP.NET 的檔案時,模組會自動執行。如需詳細資訊,請參閱 HTTP 處理常式和 HTTP 模組概觀

請參閱

概念

IIS 5.0 和 6.0 的 ASP.NET 應用程式生命週期概觀

其他資源

HTTP 模組簡介