9

我正在尝试按照ASP.NET OutputCache 和 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
4

1 回答 1

15

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
于 2013-01-28T20:40:36.423 回答