【问题标题】:Application_EndRequest not finding SessionApplication_EndRequest 未找到会话
【发布时间】:2012-12-12 20:00:59
【问题描述】:

我正在尝试按照ASP.NET OutputCache and Cookies 中的建议在 Global.asax.vb 中的 Application_EndRequest 中设置一个 cookie

我写了下面的代码,cookie 得到了ERROR 的值。

为什么会话不可用?

Sub Application_EndRequest(ByVal sender As Object, ByVal e As EventArgs)
    Dim context As HttpContext = HttpContext.Current
    If Not context.Session Is Nothing Then
        context.Response.Cookies("T").Value = context.Session("T")
    Else
        context.Response.Cookies("T").Value = "ERROR"
    End If
End Sub

【问题讨论】:

    标签: asp.net session global-asax


    【解决方案1】:

    Application_EndRequest 事件中的会话不再存在。

    Application_PostRequestHandlerExecute 在您的应用程序代码执行之后但在SessionState 释放之前调用。

    Sub Application_PostRequestHandlerExecute(ByVal sender As Object, ByVal e As EventArgs)
        Dim context As HttpContext = HttpContext.Current
        If Not context.Session Is Nothing Then
            context.Response.Cookies("T").Value = context.Session("T")
        Else
            context.Response.Cookies("T").Value = "ERROR"
        End If
    End Sub
    

    【讨论】:

    • 我正在寻找在每个 HTTP 请求之后执行的东西,我认为 Application_EndRequest 会这样做。会话不应在请求之间释放,只能在会话结束时释放。
    • 释放的不是session,而是SessionState,它的控制权返回给服务器。在您处理请求(也称为“您的代码”)之后,每个请求都会触发此事件。
    猜你喜欢
    • 2020-10-11
    • 2014-03-12
    • 2019-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多