Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Application_EndRequest not finding Session

I'm trying to set a cookie in Application_EndRequest in Global.asax.vb as suggested in ASP.NET OutputCache and Cookies

I've written the following code, cookie gets ERROR value.

Why isn't session available?

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
like image 557
JNF Avatar asked Dec 12 '12 20:12

JNF


1 Answers

The session does not exist anymore in the Application_EndRequest event.

Application_PostRequestHandlerExecute is called after the code from your application is executed but before the SessionState is released.

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
like image 162
Yan Brunet Avatar answered Nov 16 '22 03:11

Yan Brunet