HttpApplication 類別

定義

定義了 ASP.NET 應用程式中所有應用程式物件共有的方法、屬性與事件。 這個類別是 Global.asax 檔案中使用者所定義之應用程式的基類。

public ref class HttpApplication : IDisposable, System::ComponentModel::IComponent, System::Web::IHttpAsyncHandler
public class HttpApplication : IDisposable, System.ComponentModel.IComponent, System.Web.IHttpAsyncHandler
type HttpApplication = class
    interface IHttpAsyncHandler
    interface IHttpHandler
    interface IComponent
    interface IDisposable
type HttpApplication = class
    interface IComponent
    interface IDisposable
    interface IHttpAsyncHandler
    interface IHttpHandler
Public Class HttpApplication
Implements IComponent, IDisposable, IHttpAsyncHandler
繼承
HttpApplication
實作

範例

以下兩個範例說明如何使用該 HttpApplication 類別及其事件。 第一個範例示範如何建立自訂 HTTP 模組並將事件連結到它。 第二個範例示範如何修改 Web.config 檔案。

以下範例示範如何建立自訂 HTTP 模組並將事件連接到 AcquireRequestState HTTP 模組。 HTTP 模組會攔截每個對 Web 應用程式資源的請求,從而讓你能過濾用戶端請求。 任何訂閱事件 HttpApplication 的 HTTP 模組都必須實作該 IHttpModule 介面。

using System;
using System.Web;

namespace Samples.AspNet.CS
{
    public class CustomHTTPModule : IHttpModule
    {
        public CustomHTTPModule()
        {
            // Class constructor.
        }

        // Classes that inherit IHttpModule 
        // must implement the Init and Dispose methods.
        public void Init(HttpApplication app)
        {

            app.AcquireRequestState += new EventHandler(app_AcquireRequestState);
            app.PostAcquireRequestState += new EventHandler(app_PostAcquireRequestState);
        }

        public void Dispose()
        {
            // Add code to clean up the
            // instance variables of a module.
        }

        // Define a custom AcquireRequestState event handler.
        public void app_AcquireRequestState(object o, EventArgs ea)
        {
            HttpApplication httpApp = (HttpApplication)o;
            HttpContext ctx = HttpContext.Current;
            ctx.Response.Write(" Executing AcquireRequestState ");
        }

        // Define a custom PostAcquireRequestState event handler.
        public void app_PostAcquireRequestState(object o, EventArgs ea)
        {
            HttpApplication httpApp = (HttpApplication)o;
            HttpContext ctx = HttpContext.Current;
            ctx.Response.Write(" Executing PostAcquireRequestState ");
        }
    }
}
Imports System.Web

Namespace Samples.AspNet.VB
    Public Class CustomHTTPModule
        Implements IHttpModule

        Public Sub New()

            ' Class constructor.

        End Sub


        ' Classes that inherit IHttpModule 
        ' must implement the Init and Dispose methods.
        Public Sub Init(ByVal app As HttpApplication) Implements IHttpModule.Init

            AddHandler app.AcquireRequestState, AddressOf app_AcquireRequestState
            AddHandler app.PostAcquireRequestState, AddressOf app_PostAcquireRequestState

        End Sub


        Public Sub Dispose() Implements IHttpModule.Dispose

            ' Add code to clean up the
            ' instance variables of a module.

        End Sub


        ' Define a custom AcquireRequestState event handler.
        Public Sub app_AcquireRequestState(ByVal o As Object, ByVal ea As EventArgs)

            Dim httpApp As HttpApplication = CType(o, HttpApplication)
            Dim ctx As HttpContext = HttpContext.Current
            ctx.Response.Write(" Executing AcquireRequestState ")

        End Sub

        ' Define a custom PostAcquireRequestState event handler.
        Public Sub app_PostAcquireRequestState(ByVal o As Object, ByVal ea As EventArgs)

            Dim httpApp As HttpApplication = CType(o, HttpApplication)
            Dim ctx As HttpContext = HttpContext.Current
            ctx.Response.Write(" Executing PostAcquireRequestState ")

        End Sub

    End Class

End Namespace

在自訂 HTTP 模組發生事件之前,您必須修改 Web.config 檔案中的設定,以通知 ASP.NET HTTP 模組的存在。 以下範例展示了 Web.config 檔案中 httpModules 適當的設定。 以下設定適用於 IIS 7.0 經典模式及早期版本的 IIS。

<configuration>
  <system.web>
    <httpModules>
      <add type="Samples.AspNet.CS.CustomHTTPModule"
        name="CustomHttpModule" />
      </httpModules>
  </system.web>
</configuration>
<configuration>
  <system.web>
    <httpModules>
      <add type="Samples.AspNet.VB.CustomHTTPModule"
        name="CustomHttpModule" />
      </httpModules>
  </system.web>
</configuration>

以下設定適用於 IIS 7.0 整合模式。

<configuration>
  <system.webServer>
    <modules>
      <add type="Samples.AspNet.CS.CustomHTTPModule"
        name="CustomHttpModule" />
      </modules>
  </system.webServer>
</configuration>
<configuration>
  <system.webServer>
    <modules>
      <add type="Samples.AspNet.VB.CustomHTTPModule"
        name="CustomHttpModule" />
      <modules>
  </system.webServer>
</configuration>

備註

HttpApplication 類別的實例是在 ASP.NET 基礎架構中建立的,而非由使用者直接建立。 該類別的一個實例 HttpApplication 在其生命週期內會處理許多請求。 然而,它一次只能處理一個請求。 因此,成員變數可用來儲存每個請求的資料。

應用程式會引發事件,這些事件可由自訂模組(實作 IHttpModule 介面)或由 Global.asax 檔案中定義的事件處理程式處理。 實現該 IHttpModule 介面的自訂模組可以放在 App_Code 資料夾或 Bin 資料夾的 DLL 裡。

HttpApplication 於 .NET Framework 3.5 版本中引入。 欲了解更多資訊,請參閱 版本與相依關係。

Note

當以整合模式運行 IIS 7.0 時,App_Code 資料夾或 Bin 資料夾中的自訂模組會套用到請求管線中的所有請求。 Global.asax 檔案中的事件處理程式程式碼僅適用於映射到 ASP.NET 處理器的請求。

申請事件依以下順序提出:

  1. BeginRequest

  2. AuthenticateRequest

  3. PostAuthenticateRequest

  4. AuthorizeRequest

  5. PostAuthorizeRequest

  6. ResolveRequestCache

  7. PostResolveRequestCache

    事件發生 PostResolveRequestCache 後且事件發生前 PostMapRequestHandler ,會建立一個事件處理程序(即對應請求網址的頁面)。 當伺服器以整合模式運行 IIS 7.0 且至少是 .NET Framework 3.0 版本時,會觸發 MapRequestHandler 事件。 當伺服器以經典模式或較早版本的 IIS 運行 IIS 7.0 時,此事件無法被處理。

  8. PostMapRequestHandler

  9. AcquireRequestState

  10. PostAcquireRequestState

  11. PreRequestHandlerExecute

    事件處理器已被執行。

  12. PostRequestHandlerExecute

  13. ReleaseRequestState

  14. PostReleaseRequestState

    事件被觸發後 PostReleaseRequestState ,任何現有的回應過濾器會過濾輸出。

  15. UpdateRequestCache

  16. PostUpdateRequestCache

  17. LogRequest

    此事件在 IIS 7.0 整合模式及至少 .NET Framework 3.0 中均有支援

  18. PostLogRequest

    此活動支援 IIS 7.0 整合模式,至少支援 .NET Framework 3.0

  19. EndRequest

建構函式

名稱 Description
HttpApplication()

初始化 HttpApplication 類別的新執行個體。

屬性

名稱 Description
Application

取得應用程式目前的狀態。

Context

取得關於當前請求的 HTTP 特定資訊。

Events

取得處理所有應用程式事件的事件處理程序委派清單。

Modules

取得目前應用程式的模組集合。

Request

取得目前請求的內在請求物件。

Response

取得當前請求的內在回應物件。

Server

取得目前請求的內在伺服器物件。

Session

取得提供存取會話資料的內在會話物件。

Site

取得或設定一個實作的 IComponent 網站介面。

User

取得目前請求的內在使用者物件。

方法

名稱 Description
AddOnAcquireRequestStateAsync(BeginEventHandler, EndEventHandler, Object)

將指定 AcquireRequestState 事件加入當前請求的非同步 AcquireRequestState 事件處理程序集合中。

AddOnAcquireRequestStateAsync(BeginEventHandler, EndEventHandler)

將指定 AcquireRequestState 事件加入當前請求的非同步 AcquireRequestState 事件處理程序集合中。

AddOnAuthenticateRequestAsync(BeginEventHandler, EndEventHandler, Object)

將指定 AuthenticateRequest 事件加入當前請求的非同步 AuthenticateRequest 事件處理程序集合中。

AddOnAuthenticateRequestAsync(BeginEventHandler, EndEventHandler)

將指定 AuthenticateRequest 事件加入當前請求的非同步 AuthenticateRequest 事件處理程序集合中。

AddOnAuthorizeRequestAsync(BeginEventHandler, EndEventHandler, Object)

將指定 AuthorizeRequest 事件加入當前請求的非同步 AuthorizeRequest 事件處理程序集合中。

AddOnAuthorizeRequestAsync(BeginEventHandler, EndEventHandler)

將指定 AuthorizeRequest 事件加入當前請求的非同步 AuthorizeRequest 事件處理程序集合中。

AddOnBeginRequestAsync(BeginEventHandler, EndEventHandler, Object)

將指定 BeginRequest 事件加入當前請求的非同步 BeginRequest 事件處理程序集合中。

AddOnBeginRequestAsync(BeginEventHandler, EndEventHandler)

將指定 BeginRequest 事件加入當前請求的非同步 BeginRequest 事件處理程序集合中。

AddOnEndRequestAsync(BeginEventHandler, EndEventHandler, Object)

將指定 EndRequest 事件加入當前請求的非同步 EndRequest 事件處理程序集合中。

AddOnEndRequestAsync(BeginEventHandler, EndEventHandler)

將指定 EndRequest 事件加入當前請求的非同步 EndRequest 事件處理程序集合中。

AddOnLogRequestAsync(BeginEventHandler, EndEventHandler, Object)

將指定 LogRequest 事件加入當前請求的非同步 LogRequest 事件處理程序集合中。

AddOnLogRequestAsync(BeginEventHandler, EndEventHandler)

將指定 LogRequest 事件加入當前請求的非同步 LogRequest 事件處理程序集合中。

AddOnMapRequestHandlerAsync(BeginEventHandler, EndEventHandler, Object)

將指定 MapRequestHandler 事件加入當前請求的非同步 MapRequestHandler 事件處理程序集合中。

AddOnMapRequestHandlerAsync(BeginEventHandler, EndEventHandler)

將指定 MapRequestHandler 事件加入當前請求的非同步 MapRequestHandler 事件處理程序集合中。

AddOnPostAcquireRequestStateAsync(BeginEventHandler, EndEventHandler, Object)

將指定 PostAcquireRequestState 事件加入當前請求的非同步 PostAcquireRequestState 事件處理程序集合中。

AddOnPostAcquireRequestStateAsync(BeginEventHandler, EndEventHandler)

將指定 PostAcquireRequestState 事件加入當前請求的非同步 PostAcquireRequestState 事件處理程序集合中。

AddOnPostAuthenticateRequestAsync(BeginEventHandler, EndEventHandler, Object)

將指定 PostAuthorizeRequest 事件加入當前請求的非同步 PostAuthorizeRequest 事件處理程序集合中。

AddOnPostAuthenticateRequestAsync(BeginEventHandler, EndEventHandler)

將指定 PostAuthenticateRequest 事件加入當前請求的非同步 PostAuthenticateRequest 事件處理程序集合中。

AddOnPostAuthorizeRequestAsync(BeginEventHandler, EndEventHandler, Object)

將指定 PostAuthorizeRequest 值加入當前請求的非同步 PostAuthorizeRequest 事件處理程序集合中。

AddOnPostAuthorizeRequestAsync(BeginEventHandler, EndEventHandler)

將指定 PostAuthorizeRequest 事件加入當前請求的非同步 PostAuthorizeRequest 事件處理程序集合中。

AddOnPostLogRequestAsync(BeginEventHandler, EndEventHandler, Object)

將指定 PostLogRequest 事件加入當前請求的非同步 PostLogRequest 事件處理程序集合中。

AddOnPostLogRequestAsync(BeginEventHandler, EndEventHandler)

將指定 PostLogRequest 事件加入當前請求的非同步 PostLogRequest 事件處理程序集合中。

AddOnPostMapRequestHandlerAsync(BeginEventHandler, EndEventHandler, Object)

將指定 PostMapRequestHandler 事件加入當前請求的非同步 PostMapRequestHandler 事件處理程序集合中。

AddOnPostMapRequestHandlerAsync(BeginEventHandler, EndEventHandler)

將指定 PostMapRequestHandler 事件加入當前請求的非同步 PostMapRequestHandler 事件處理程序集合中。

AddOnPostReleaseRequestStateAsync(BeginEventHandler, EndEventHandler, Object)

將指定 PostReleaseRequestState 事件加入當前請求的非同步 PostReleaseRequestState 事件處理程序集合中。

AddOnPostReleaseRequestStateAsync(BeginEventHandler, EndEventHandler)

將指定 PostReleaseRequestState 事件加入當前請求的非同步 PostReleaseRequestState 事件處理程序集合中。

AddOnPostRequestHandlerExecuteAsync(BeginEventHandler, EndEventHandler, Object)

將指定 PostRequestHandlerExecute 事件加入當前請求的非同步 PostRequestHandlerExecute 事件處理程序集合中。

AddOnPostRequestHandlerExecuteAsync(BeginEventHandler, EndEventHandler)

將指定 PostRequestHandlerExecute 事件加入當前請求的非同步 PostRequestHandlerExecute 事件處理程序集合中。

AddOnPostResolveRequestCacheAsync(BeginEventHandler, EndEventHandler, Object)

將指定 PostResolveRequestCache 事件加入當前請求的非同步 PostResolveRequestCache 事件處理程序集合中。

AddOnPostResolveRequestCacheAsync(BeginEventHandler, EndEventHandler)

將指定 PostResolveRequestCache 事件加入當前請求的非同步 PostResolveRequestCache 事件處理程序集合中。

AddOnPostUpdateRequestCacheAsync(BeginEventHandler, EndEventHandler, Object)

將指定 PostUpdateRequestCache 事件加入當前請求的非同步 PostUpdateRequestCache 事件處理程序集合中。

AddOnPostUpdateRequestCacheAsync(BeginEventHandler, EndEventHandler)

將指定 PostUpdateRequestCache 事件加入當前請求的非同步 PostUpdateRequestCache 事件處理程序集合中。

AddOnPreRequestHandlerExecuteAsync(BeginEventHandler, EndEventHandler, Object)

將指定 PreRequestHandlerExecute 事件加入當前請求的非同步 PreRequestHandlerExecute 事件處理程序集合中。

AddOnPreRequestHandlerExecuteAsync(BeginEventHandler, EndEventHandler)

將指定 PreRequestHandlerExecute 事件加入當前請求的非同步 PreRequestHandlerExecute 事件處理程序集合中。

AddOnReleaseRequestStateAsync(BeginEventHandler, EndEventHandler, Object)

將指定 ReleaseRequestState 事件加入當前請求的非同步 ReleaseRequestState 事件處理程序集合中。

AddOnReleaseRequestStateAsync(BeginEventHandler, EndEventHandler)

將指定 ReleaseRequestState 事件加入當前請求的非同步 ReleaseRequestState 事件處理程序集合中。

AddOnResolveRequestCacheAsync(BeginEventHandler, EndEventHandler, Object)

將指定的 ResolveRequestCache 事件處理器加入當前請求的非同步 ResolveRequestCache 事件處理器集合中。

AddOnResolveRequestCacheAsync(BeginEventHandler, EndEventHandler)

將指定的 ResolveRequestCache 事件處理器加入當前請求的非同步 ResolveRequestCache 事件處理器集合中。

AddOnUpdateRequestCacheAsync(BeginEventHandler, EndEventHandler, Object)

將指定 UpdateRequestCache 事件加入當前請求的非同步 UpdateRequestCache 事件處理程序集合中。

AddOnUpdateRequestCacheAsync(BeginEventHandler, EndEventHandler)

將指定 UpdateRequestCache 事件加入當前請求的非同步 UpdateRequestCache 事件處理程序集合中。

CompleteRequest()

這會讓 ASP.NET 繞過 HTTP 管線執行鏈中的所有事件和過濾,直接執行 EndRequest 事件。

Dispose()

處理該 HttpApplication 實例。

Equals(Object)

判斷指定的 物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetOutputCacheProviderName(HttpContext)

取得為網站設定的預設輸出快取提供者名稱。

GetType()

取得目前實例的 Type

(繼承來源 Object)
GetVaryByCustomString(HttpContext, String)

提供該屬性的全應用實作 VaryByCustom

Init()

在所有事件處理模組加入後,執行自訂初始化程式碼。

MemberwiseClone()

建立目前 Object的淺層複本。

(繼承來源 Object)
OnExecuteRequestStep(Action<HttpContextBase,Action>)

指定在執行請求步驟時呼叫回用。

RegisterModule(Type)

註冊一個應用程式模組。

ToString()

傳回表示目前 物件的字串。

(繼承來源 Object)

事件

名稱 Description
AcquireRequestState

當 ASP.NET 取得與當前請求相關的當前狀態(例如會話狀態)時,會發生這種情況。

AuthenticateRequest

當安全模組已確認使用者身份時,會發生這種情況。

AuthorizeRequest

當安全模組已驗證使用者授權時,會發生這種情況。

BeginRequest

當 ASP.NET 回應請求時,作為 HTTP 管線執行鏈中的第一個事件發生。

Disposed

當應用被處置時發生。

EndRequest

當 ASP.NET 回應請求時,作為 HTTP 管線執行鏈中的最後一個事件發生。

Error

當拋出未處理的例外時會發生。

LogRequest

此事發生在 ASP.NET 對當前請求進行任何日誌記錄之前。

MapRequestHandler

當處理器被選中回應請求時發生。

PostAcquireRequestState

當已取得與目前請求相關的請求狀態(例如會話狀態)時,會發生這種情況。

PostAuthenticateRequest

當安全模組已確認使用者身份時,會發生這種情況。

PostAuthorizeRequest

當當前請求的使用者已被授權時,會發生這種情況。

PostLogRequest

當 ASP.NET 完成處理所有 LogRequest 事件的處理程序時。

PostMapRequestHandler

當 ASP.NET 將當前請求映射到適當的事件處理器時,會發生。

PostReleaseRequestState

當 ASP.NET 完成執行所有請求事件處理程序且請求狀態資料已儲存時,會發生此現象。

PostRequestHandlerExecute

當 ASP.NET 事件處理程序(例如頁面或 XML 網路服務)執行結束時,會發生。

PostResolveRequestCache

當 ASP.NET 繞過目前事件處理程序的執行,允許快取模組從快取中執行請求時,會發生這種情況。

PostUpdateRequestCache

當 ASP.NET 完成快取模組更新並儲存用於從快取中處理後續請求的回應時。

PreRequestHandlerExecute

發生在 ASP.NET 開始執行事件處理程序(例如頁面或 XML Web 服務)之前。

PreSendRequestContent

此事發生在 ASP.NET 將內容傳送給用戶端之前。

PreSendRequestHeaders

此現象發生在 ASP.NET 向用戶端傳送 HTTP 標頭之前。

ReleaseRequestState

發生在 ASP.NET 執行完所有請求事件處理器後。 此事件會使狀態模組儲存當前狀態資料。

RequestCompleted

當與請求相關的受管理物件已被釋放時,會發生這種情況。

ResolveRequestCache

當 ASP.NET 完成授權事件,讓快取模組從快取中服務請求時,會繞過事件處理程序(例如頁面或 XML Web 服務)的執行。

UpdateRequestCache

當 ASP.NET 完成事件處理程序執行,讓快取模組儲存回應,進而用於從快取中處理後續請求時。

明確介面實作

名稱 Description
IHttpAsyncHandler.BeginProcessRequest(HttpContext, AsyncCallback, Object)

會對 HTTP 事件處理器發起非同步呼叫。

IHttpAsyncHandler.EndProcessRequest(IAsyncResult)

當流程結束時,提供非同步的處理 End 方法。

IHttpHandler.IsReusable

會獲得 Boolean 一個值,表示是否有其他請求可以使用該 IHttpHandler 物件。

IHttpHandler.ProcessRequest(HttpContext)

允許由自訂的 HTTP 處理器處理 HTTP Web 請求,該處理器實作了該 IHttpHandler 介面。

適用於

另請參閱