SessionStateUtility.GetHttpSessionStateFromContext(HttpContext) Metodo

Definizione

Recupera i dati della sessione dal contesto per la richiesta corrente.

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

Parametri

context
HttpContext

Oggetto HttpContext da cui recuperare i dati della sessione.

Restituisce

Un'istanza dell'implementazione IHttpSessionState nella quale sono stati inseriti i dati della sessione dalla richiesta corrente.

Esempio

Nell'esempio di codice seguente viene illustrato il gestore dell'evento ReleaseRequestState in un modulo di stato sessione personalizzato. Il modulo recupera i dati della sessione dall'oggetto HttpContext per la richiesta corrente usando il GetHttpSessionStateFromContext metodo . Questo esempio di codice fa parte di un esempio più grande fornito per la SessionStateUtility classe.

//
// 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

Commenti

Il GetHttpSessionStateFromContext metodo può essere usato da un modulo di stato sessione per recuperare i dati della sessione dalla richiesta corrente. Ciò si verifica durante l'evento ReleaseRequestState alla fine di una richiesta. I dati della sessione restituiti possono quindi essere scritti nell'archivio dati della sessione. Se la sessione è stata abbandonata, è possibile rimuovere i dati della sessione dall'archivio dati e , HttpContexte l'evento Session_OnEnd può essere eseguito.

Note per gli eredi

È possibile usare il RemoveHttpSessionStateFromContext(HttpContext) metodo per rimuovere i dati della sessione dall'archivio interno e il RaiseSessionEnd(IHttpSessionState, Object, EventArgs) metodo per generare l'evento Session_OnEnd .

Si applica a