Bagikan melalui


SessionStateUtility.GetHttpSessionStateFromContext(HttpContext) Metode

Definisi

Mengambil data sesi dari konteks untuk permintaan saat ini.

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

Parameter

context
HttpContext

dari HttpContext mana untuk mengambil data sesi.

Mengembalikan

IHttpSessionState Instans implementasi yang diisi dengan data sesi dari permintaan saat ini.

Contoh

Contoh kode berikut menunjukkan handler untuk ReleaseRequestState peristiwa dalam modul keadaan sesi kustom. Modul mengambil data sesi dari HttpContext untuk permintaan saat ini menggunakan GetHttpSessionStateFromContext metode . Contoh kode ini adalah bagian dari contoh yang lebih besar yang disediakan untuk SessionStateUtility kelas .

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

Keterangan

Metode GetHttpSessionStateFromContext ini dapat digunakan oleh modul status sesi untuk mengambil data sesi dari permintaan saat ini. Ini terjadi selama ReleaseRequestState peristiwa di akhir permintaan. Data sesi yang dikembalikan kemudian dapat ditulis ke penyimpanan data sesi. Jika sesi telah ditinggalkan, data sesi dapat dihapus dari penyimpanan data dan HttpContext, dan peristiwa Session_OnEnd dapat dijalankan.

Catatan Bagi Inheritor

Anda dapat menggunakan RemoveHttpSessionStateFromContext(HttpContext) metode untuk menghapus data sesi dari penyimpanan internal, dan RaiseSessionEnd(IHttpSessionState, Object, EventArgs) metode untuk meningkatkan Session_OnEnd peristiwa.

Berlaku untuk