共用方式為


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 。 模組會使用 GetHttpSessionStateFromContext 方法,從 HttpContext 中擷取目前要求的會話資料。 此程式碼範例是針對 類別提供的較大範例的 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 事件的方法。

適用於