Application_EndRequest未找到Session

时间:2012-12-12 20:01:00

标签: asp.net session global-asax

我正在尝试按照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

1 个答案:

答案 0 :(得分:13)

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