SessionStateUtility.GetHttpSessionStateFromContext(HttpContext) 方法

定義

從當前請求的上下文中取得會話資料。

public:
 static System::Web::SessionState::IHttpSessionState ^ GetHttpSessionStateFromContext(System::Web::HttpContext ^ context);
public static System.Web.SessionState.IHttpSessionState GetHttpSessionStateFromContext(System.Web.HttpContext context);
static member GetHttpSessionStateFromContext : System.Web.HttpContext -> System.Web.SessionState.IHttpSessionState
Public Shared Function GetHttpSessionStateFromContext (context As HttpContext) As IHttpSessionState

參數

context
HttpContext

HttpContext 中取得會話資料。

傳回

一個 IHttpSessionState 實作實例,填充來自當前請求的會話資料。

範例

以下程式碼範例展示了該事件在自訂會話狀態模組中的處理 ReleaseRequestState 程序。 模組會利用 HttpContext 該方法從 中GetHttpSessionStateFromContext取得當前請求的會話資料。 此程式碼範例是本類別更大範例 SessionStateUtility 的一部分。

//
// Event handler for HttpApplication.ReleaseRequestState
//

private void OnReleaseRequestState(object source, EventArgs args)
{
    HttpApplication app = (HttpApplication)source;
    HttpContext context = app.Context;
    string sessionID;

    // Read the session state from the context
    HttpSessionStateContainer stateProvider =
      (HttpSessionStateContainer)(SessionStateUtility.GetHttpSessionStateFromContext(context));

    // If Session.Abandon() was called, remove the session data from the local Hashtable
    // and execute the Session_OnEnd event from the Global.asax file.
    if (stateProvider.IsAbandoned)
    {
        try
        {
            pHashtableLock.AcquireWriterLock(Int32.MaxValue);

            sessionID = pSessionIDManager.GetSessionID(context);
            pSessionItems.Remove(sessionID);
        }
        finally
        {
            pHashtableLock.ReleaseWriterLock();
        }

        SessionStateUtility.RaiseSessionEnd(stateProvider, this, EventArgs.Empty);
    }

    SessionStateUtility.RemoveHttpSessionStateFromContext(context);
}
'
' Event handler for HttpApplication.ReleaseRequestState
'
Private Sub OnReleaseRequestState(ByVal [source] As Object, ByVal args As EventArgs)
    Dim app As HttpApplication = CType([source], HttpApplication)
    Dim context As HttpContext = app.Context
    Dim sessionID As String

    ' Read the session state from the context
    Dim stateProvider As HttpSessionStateContainer = _
       CType(SessionStateUtility.GetHttpSessionStateFromContext(context), HttpSessionStateContainer)

    ' If Session.Abandon() was called, remove the session data from the local Hashtable
    ' and execute the Session_OnEnd event from the Global.asax file.
    If stateProvider.IsAbandoned Then
        Try
            pHashtableLock.AcquireWriterLock(Int32.MaxValue)

            sessionID = pSessionIDManager.GetSessionID(context)
            pSessionItems.Remove(sessionID)
        Finally
            pHashtableLock.ReleaseWriterLock()
        End Try

        SessionStateUtility.RaiseSessionEnd(stateProvider, Me, EventArgs.Empty)
    End If

  SessionStateUtility.RemoveHttpSessionStateFromContext(context)
End Sub

備註

GetHttpSessionStateFromContext 方法可由會話狀態模組用來從當前請求中擷取會話資料。 這會在請求結束時的事件中發生 ReleaseRequestState 。 回傳的會話資料接著可以寫入會話資料儲存庫。 若會話已被放棄,則可從資料儲存庫 和 HttpContext中移除會話資料,並執行 Session_OnEnd 事件。

給繼承者的注意事項

你可以用 RemoveHttpSessionStateFromContext(HttpContext) 移除會話資料的方法,以及 RaiseSessionEnd(IHttpSessionState, Object, EventArgs) 提出 Session_OnEnd 事件的方法。

適用於