Global.asax event that has access to session state

Shadowxvii picture Shadowxvii · Mar 27, 2012 · Viewed 16.2k times · Source

I'm trying to access the session state in my global.asax for every request (page, documents, PDF, etc.). I know i can't do that in Application_BeginRequest, and i thought i could in Application_AcquireRequestState, but it won't works, which is strange because it works in another project.

So, i'm looking for an event in which i would always have access to the session state for every request.

Thanks

EDIT: @Mike

I tried doing this

Sub Application_PreRequestHandlerExecute(ByVal sender As Object, ByVal e As EventArgs)
    Session("test") = "test"
End Sub

But i still get errors as i don't have access to session state.

Answer

Mike picture Mike · Mar 27, 2012

The session gets loaded during Application_AcquireRequestState. Your safe bet is to build Application_PreRequestHandlerExecute and access it there.


Update: Not every request has a session state. You need to also check for null: if (System.Web.HttpContext.Current.Session != null).