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 版引進。 如需詳細資訊,請參閱版本和相依性

注意

在整合模式中執行 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 ,就會建立事件處理常式 (,這是對應至要求 URL) 的頁面。 當伺服器以整合模式執行 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

建構函式

HttpApplication()

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

屬性

Application

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

Context

取得有關目前要求的 HTTP 特有資訊。

Events

取得處理所有應用程式事件的事件處理常式委派 (Delegate) 的清單。

Modules

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

Request

取得目前要求的內建要求物件。

Response

取得目前要求的內建回應物件。

Server

取得目前要求的內建伺服器物件。

Session

取得提供工作階段資料存取的內建工作階段物件。

Site

取得或設定 IComponent 實作的位置介面。

User

取得目前要求的內建使用者物件。

方法

AddOnAcquireRequestStateAsync(BeginEventHandler, EndEventHandler)

將指定的 AcquireRequestState 事件加入目前要求之非同步 AcquireRequestState 事件處理常式的集合。

AddOnAcquireRequestStateAsync(BeginEventHandler, EndEventHandler, Object)

將指定的 AcquireRequestState 事件加入目前要求之非同步 AcquireRequestState 事件處理常式的集合。

AddOnAuthenticateRequestAsync(BeginEventHandler, EndEventHandler)

將指定的 AuthenticateRequest 事件加入目前要求之非同步 AuthenticateRequest 事件處理常式的集合。

AddOnAuthenticateRequestAsync(BeginEventHandler, EndEventHandler, Object)

將指定的 AuthenticateRequest 事件加入目前要求之非同步 AuthenticateRequest 事件處理常式的集合。

AddOnAuthorizeRequestAsync(BeginEventHandler, EndEventHandler)

將指定的 AuthorizeRequest 事件加入目前要求之非同步 AuthorizeRequest 事件處理常式的集合。

AddOnAuthorizeRequestAsync(BeginEventHandler, EndEventHandler, Object)

將指定的 AuthorizeRequest 事件加入目前要求之非同步 AuthorizeRequest 事件處理常式的集合。

AddOnBeginRequestAsync(BeginEventHandler, EndEventHandler)

將指定的 BeginRequest 事件加入目前要求之非同步 BeginRequest 事件處理常式的集合。

AddOnBeginRequestAsync(BeginEventHandler, EndEventHandler, Object)

將指定的 BeginRequest 事件加入目前要求之非同步 BeginRequest 事件處理常式的集合。

AddOnEndRequestAsync(BeginEventHandler, EndEventHandler)

將指定的 EndRequest 事件加入目前要求之非同步 EndRequest 事件處理常式的集合。

AddOnEndRequestAsync(BeginEventHandler, EndEventHandler, Object)

將指定的 EndRequest 事件加入目前要求之非同步 EndRequest 事件處理常式的集合。

AddOnLogRequestAsync(BeginEventHandler, EndEventHandler)

將指定的 LogRequest 事件加入目前要求之非同步 LogRequest 事件處理常式的集合。

AddOnLogRequestAsync(BeginEventHandler, EndEventHandler, Object)

將指定的 LogRequest 事件加入目前要求之非同步 LogRequest 事件處理常式的集合。

AddOnMapRequestHandlerAsync(BeginEventHandler, EndEventHandler)

將指定的 MapRequestHandler 事件加入目前要求之非同步 MapRequestHandler 事件處理常式的集合。

AddOnMapRequestHandlerAsync(BeginEventHandler, EndEventHandler, Object)

將指定的 MapRequestHandler 事件加入目前要求之非同步 MapRequestHandler 事件處理常式的集合。

AddOnPostAcquireRequestStateAsync(BeginEventHandler, EndEventHandler)

將指定的 PostAcquireRequestState 事件加入目前要求之非同步 PostAcquireRequestState 事件處理常式的集合。

AddOnPostAcquireRequestStateAsync(BeginEventHandler, EndEventHandler, Object)

將指定的 PostAcquireRequestState 事件加入目前要求之非同步 PostAcquireRequestState 事件處理常式的集合。

AddOnPostAuthenticateRequestAsync(BeginEventHandler, EndEventHandler)

將指定的 PostAuthenticateRequest 事件加入目前要求之非同步 PostAuthenticateRequest 事件處理常式的集合。

AddOnPostAuthenticateRequestAsync(BeginEventHandler, EndEventHandler, Object)

將指定的 PostAuthorizeRequest 事件加入目前要求之非同步 PostAuthorizeRequest 事件處理常式的集合。

AddOnPostAuthorizeRequestAsync(BeginEventHandler, EndEventHandler)

將指定的 PostAuthorizeRequest 事件加入目前要求之非同步 PostAuthorizeRequest 事件處理常式的集合。

AddOnPostAuthorizeRequestAsync(BeginEventHandler, EndEventHandler, Object)

將指定的 PostAuthorizeRequest 加入目前要求之非同步 PostAuthorizeRequest 事件處理常式的集合。

AddOnPostLogRequestAsync(BeginEventHandler, EndEventHandler)

將指定的 PostLogRequest 事件加入目前要求之非同步 PostLogRequest 事件處理常式的集合。

AddOnPostLogRequestAsync(BeginEventHandler, EndEventHandler, Object)

將指定的 PostLogRequest 事件加入目前要求之非同步 PostLogRequest 事件處理常式的集合。

AddOnPostMapRequestHandlerAsync(BeginEventHandler, EndEventHandler)

將指定的 PostMapRequestHandler 事件加入目前要求之非同步 PostMapRequestHandler 事件處理常式的集合。

AddOnPostMapRequestHandlerAsync(BeginEventHandler, EndEventHandler, Object)

將指定的 PostMapRequestHandler 事件加入目前要求之非同步 PostMapRequestHandler 事件處理常式的集合。

AddOnPostReleaseRequestStateAsync(BeginEventHandler, EndEventHandler)

將指定的 PostReleaseRequestState 事件加入目前要求之非同步 PostReleaseRequestState 事件處理常式的集合。

AddOnPostReleaseRequestStateAsync(BeginEventHandler, EndEventHandler, Object)

將指定的 PostReleaseRequestState 事件加入目前要求之非同步 PostReleaseRequestState 事件處理常式的集合。

AddOnPostRequestHandlerExecuteAsync(BeginEventHandler, EndEventHandler)

將指定的 PostRequestHandlerExecute 事件加入目前要求之非同步 PostRequestHandlerExecute 事件處理常式的集合。

AddOnPostRequestHandlerExecuteAsync(BeginEventHandler, EndEventHandler, Object)

將指定的 PostRequestHandlerExecute 事件加入目前要求之非同步 PostRequestHandlerExecute 事件處理常式的集合。

AddOnPostResolveRequestCacheAsync(BeginEventHandler, EndEventHandler)

將指定的 PostResolveRequestCache 事件加入目前要求之非同步 PostResolveRequestCache 事件處理常式的集合。

AddOnPostResolveRequestCacheAsync(BeginEventHandler, EndEventHandler, Object)

將指定的 PostResolveRequestCache 事件加入目前要求之非同步 PostResolveRequestCache 事件處理常式的集合。

AddOnPostUpdateRequestCacheAsync(BeginEventHandler, EndEventHandler)

將指定的 PostUpdateRequestCache 事件加入目前要求之非同步 PostUpdateRequestCache 事件處理常式的集合。

AddOnPostUpdateRequestCacheAsync(BeginEventHandler, EndEventHandler, Object)

將指定的 PostUpdateRequestCache 事件加入目前要求之非同步 PostUpdateRequestCache 事件處理常式的集合。

AddOnPreRequestHandlerExecuteAsync(BeginEventHandler, EndEventHandler)

將指定的 PreRequestHandlerExecute 事件加入目前要求之非同步 PreRequestHandlerExecute 事件處理常式的集合。

AddOnPreRequestHandlerExecuteAsync(BeginEventHandler, EndEventHandler, Object)

將指定的 PreRequestHandlerExecute 事件加入目前要求之非同步 PreRequestHandlerExecute 事件處理常式的集合。

AddOnReleaseRequestStateAsync(BeginEventHandler, EndEventHandler)

將指定的 ReleaseRequestState 事件加入目前要求之非同步 ReleaseRequestState 事件處理常式的集合。

AddOnReleaseRequestStateAsync(BeginEventHandler, EndEventHandler, Object)

將指定的 ReleaseRequestState 事件加入目前要求之非同步 ReleaseRequestState 事件處理常式的集合。

AddOnResolveRequestCacheAsync(BeginEventHandler, EndEventHandler)

將指定的 ResolveRequestCache 事件處理常式加入目前要求之非同步 ResolveRequestCache 事件處理常式的集合。

AddOnResolveRequestCacheAsync(BeginEventHandler, EndEventHandler, Object)

將指定的 ResolveRequestCache 事件處理常式加入目前要求之非同步 ResolveRequestCache 事件處理常式的集合。

AddOnUpdateRequestCacheAsync(BeginEventHandler, EndEventHandler)

將指定的 UpdateRequestCache 事件加入目前要求之非同步 UpdateRequestCache 事件處理常式的集合。

AddOnUpdateRequestCacheAsync(BeginEventHandler, EndEventHandler, Object)

將指定的 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)

事件

AcquireRequestState

當 ASP.NET 取得與目前要求關聯的目前狀態 (例如,工作階段狀態) 時發生。

AuthenticateRequest

發生於安全性模組建立使用者的識別 (Identity) 時。

AuthorizeRequest

發生於安全性模式驗證使用者授權時。

BeginRequest

發生於 ASP.NET 回應要求時,做為 HTTP 管線的執行鏈結裡的第一個事件。

Disposed

發生於處置應用程式時。

EndRequest

發生於 ASP.NET 回應要求時,做為 HTTP 管線的執行鏈結裡的最後一個事件。

Error

當擲回未處理的例外狀況時發生。

LogRequest

發生於 ASP.NET 執行目前要求的任何記錄之前。

MapRequestHandler

發生於選取處理常式以回應要求時。

PostAcquireRequestState

當取得與目前要求關聯的要求狀態 (例如,工作階段狀態) 時發生。

PostAuthenticateRequest

發生於安全性模組建立使用者的識別 (Identity) 時。

PostAuthorizeRequest

當目前要求的使用者已獲授權時發生。

PostLogRequest

發生於 ASP.NET 完成處理 LogRequest 事件的所有事件處理常式時。

PostMapRequestHandler

當 ASP.NET 已對應目前要求至適當事件處理常式時發生。

PostReleaseRequestState

當 ASP.NET 已完成執行所有要求事件處理常式並已儲存要求狀態資料時發生。

PostRequestHandlerExecute

當 ASP.NET 事件處理常式 (例如,網頁或 XML Web Service) 完成執行時發生。

PostResolveRequestCache

當 ASP.NET 略過目前事件處理常式的執行並允許快取模組從快取中服務要求時發生。

PostUpdateRequestCache

當 ASP.NET 完成更新快取模組並儲存回應 (用來從快取中服務後續的要求) 時發生。

PreRequestHandlerExecute

正好發生於 ASP.NET 開始執行事件處理常式 (例如,網頁或 XML Web Service) 之前。

PreSendRequestContent

正好發生於 ASP.NET 傳送內容給用戶端時。

PreSendRequestHeaders

正好發生於 ASP.NET 傳送 HTTP 標頭給用戶端時。

ReleaseRequestState

發生於 ASP.NET 完成所有要求事件處理常式的執行之後。 這個事件會造成狀態模組儲存目前的狀態資料。

RequestCompleted

已經釋放與要求相關聯之 Managed 物件時發生。

ResolveRequestCache

發生於 ASP.NET 完成授權事件,讓快取模組服務來自快取的要求,並略過事件處理常式 (例如,網頁或 XML Web Service) 的執行時。

UpdateRequestCache

發生於 ASP.NET 完成執行事件處理常式,為了讓快取模組儲存會用於服務後續來自快取的要求的回應時。

明確介面實作

IHttpAsyncHandler.BeginProcessRequest(HttpContext, AsyncCallback, Object)

啟始對 HTTP 事件處理常式的非同步呼叫。

IHttpAsyncHandler.EndProcessRequest(IAsyncResult)

處理序完成時,提供非同步處理序 End 方法。

IHttpHandler.IsReusable

取得 Boolean 值,指出另一個要求是否可以使用 IHttpHandler 物件。

IHttpHandler.ProcessRequest(HttpContext)

以實作 IHttpHandler 介面的自訂 HTTP 處理常式,來啟用 HTTP Web 要求的處理。

適用於

另請參閱