HttpApplication 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
定義了 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 處理器的請求。
申請事件依以下順序提出:
-
事件發生 PostResolveRequestCache 後且事件發生前 PostMapRequestHandler ,會建立一個事件處理程序(即對應請求網址的頁面)。 當伺服器以整合模式運行 IIS 7.0 且至少是 .NET Framework 3.0 版本時,會觸發 MapRequestHandler 事件。 當伺服器以經典模式或較早版本的 IIS 運行 IIS 7.0 時,此事件無法被處理。
-
事件處理器已被執行。
-
事件被觸發後 PostReleaseRequestState ,任何現有的回應過濾器會過濾輸出。
-
此事件在 IIS 7.0 整合模式及至少 .NET Framework 3.0 中均有支援
-
此活動支援 IIS 7.0 整合模式,至少支援 .NET Framework 3.0
建構函式
| 名稱 | Description |
|---|---|
| HttpApplication() |
初始化 HttpApplication 類別的新執行個體。 |
屬性
| 名稱 | Description |
|---|---|
| Application |
取得應用程式目前的狀態。 |
| Context |
取得關於當前請求的 HTTP 特定資訊。 |
| Events |
取得處理所有應用程式事件的事件處理程序委派清單。 |
| Modules |
取得目前應用程式的模組集合。 |
| Request |
取得目前請求的內在請求物件。 |
| Response |
取得當前請求的內在回應物件。 |
| Server |
取得目前請求的內在伺服器物件。 |
| Session |
取得提供存取會話資料的內在會話物件。 |
| Site |
取得或設定一個實作的 IComponent 網站介面。 |
| User |
取得目前請求的內在使用者物件。 |
方法
事件
| 名稱 | 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) |
當流程結束時,提供非同步的處理 |
| IHttpHandler.IsReusable |
會獲得 |
| IHttpHandler.ProcessRequest(HttpContext) |
允許由自訂的 HTTP 處理器處理 HTTP Web 請求,該處理器實作了該 IHttpHandler 介面。 |