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 事件。

适用于