HttpContext.CurrentNotification 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
會得到 RequestNotification 一個表示目前 HttpApplication 正在處理事件的值。
public:
property System::Web::RequestNotification CurrentNotification { System::Web::RequestNotification get(); };
public System.Web.RequestNotification CurrentNotification { get; }
member this.CurrentNotification : System.Web.RequestNotification
Public ReadOnly Property CurrentNotification As RequestNotification
屬性值
這是其中一項 RequestNotification 價值。
例外狀況
此操作需要在 IIS 7.0 以及至少 .NET Framework 3.0 版本中整合管線模式。
範例
以下範例示範如何利用該 CurrentNotification 屬性判斷正在處理當前請求的物件中哪個事件 HttpApplication 。 在這個範例中,事件處理器處理物件的 HttpApplication 多個事件,而該 CurrentNotification 屬性決定每個事件會被調用哪些程式碼。
using System;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
// Module that demonstrates one event handler for several events.
namespace Samples
{
public class ModuleExampleTestCS : IHttpModule
{
public ModuleExampleTestCS()
{
// Constructor
}
public void Init(HttpApplication app)
{
app.AuthenticateRequest += new EventHandler(App_Handler);
app.PostAuthenticateRequest += new EventHandler(App_Handler);
app.LogRequest += new EventHandler(App_Handler);
app.PostLogRequest += new EventHandler(App_Handler);
}
public void Dispose()
{
}
// One handler for AuthenticationRequest, PostAuthenticateRequest,
// LogRequest, and PostLogRequest events
public void App_Handler(object source, EventArgs e)
{
HttpApplication app = (HttpApplication)source;
HttpContext context = app.Context;
if (context.CurrentNotification == RequestNotification.AuthenticateRequest)
{
if (!context.IsPostNotification)
{
// Put code here that is invoked when the AuthenticateRequest event is raised.
}
else
{
// PostAuthenticateRequest
// Put code here that runs after the AuthenticateRequest event completes.
}
}
if (context.CurrentNotification == RequestNotification.LogRequest)
{
if (!context.IsPostNotification)
{
// Put code here that is invoked when the LogRequest event is raised.
}
else
{
// PostLogRequest
// Put code here that runs after the LogRequest event completes.
}
}
}
}
}
Imports System.Data
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
' Module that demonstrates one event handler for several events.
Namespace Samples
Public Class ModuleExampleTestVB
Implements IHttpModule
Public Sub New()
' Constructor
End Sub
Public Sub Init(ByVal app As HttpApplication) Implements IHttpModule.Init
AddHandler app.AuthenticateRequest, AddressOf Me.App_Handler
AddHandler app.PostAuthenticateRequest, AddressOf Me.App_Handler
AddHandler app.LogRequest, AddressOf Me.App_Handler
AddHandler app.PostLogRequest, AddressOf Me.App_Handler
End Sub
Public Sub Dispose() Implements IHttpModule.Dispose
End Sub
' One handler for AuthenticationRequest, PostAuthenticateRequest,
' LogRequest, and PostLogRequest events
Public Sub App_Handler(ByVal source As Object, ByVal e As EventArgs)
Dim app As HttpApplication = CType(source, HttpApplication)
Dim context As HttpContext = app.Context
If (context.CurrentNotification = RequestNotification.AuthenticateRequest) Then
If Not (context.IsPostNotification) Then
' Put code here that is invoked when the AuthenticateRequest event is raised.
Else
' PostAuthenticateRequest
' Put code here that runs after the AuthenticateRequest event completes.
End If
End If
If (context.CurrentNotification = RequestNotification.LogRequest) Then
If Not (context.IsPostNotification) Then
' Put code here that is invoked when the LogRequest event is raised.
Else
' PostLogRequest
' Put code here that runs after the LogRequest event completes.
End If
End If
End Sub
End Class
End Namespace
備註
此 CurrentNotification 特性需要 IIS 7.0 及至少 .NET Framework 3.0 版本中的整合管線模式。 當房產可用時,會回傳一個 RequestNotification 價值。 屬性的CurrentNotification值表示目前在實例中處理請求的事件是哪個。HttpApplication
該 CurrentNotification 屬性並非設計用來設定的。 取而代之的是,IIS 7.0 在 ASP.NET 管線中處理請求時設定。 設定該 CurrentNotification 屬性會導致編譯錯誤。
CurrentNotification 在 .NET Framework 3.5 版本中引入。 欲了解更多資訊,請參閱 版本與相依關係。