Accessing session variable values from the 'Application_BeginRequest' handler

S

Stu

Hi,

I am trying to access session variable value from the
'Application_BeginRequest' handler in the global asax file but get the
message - 'Session state not available in this context'. I have tried
loading the current context using 'Dim context As HttpContext =
HttpContext.Current' but get the error 'Object reference not set to an
instance of an object'

Anyone know how you get at this?

Thanks in advance,

Stu

Code:
Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
Dim context As HttpContext = HttpContext.Current
If Not IsNothing(context.Session("Test")) Then
context.Session("Test") = "Hello world!"
End If
End Sub
 
B

Brock Allen

Session state isn't loaded until later in the lifetime of the request. Instead
of Application_BeginRequest, build Application_PreRequestHandlerExecute --
this is the safest place to do your work prior to the page getting called.
The step where session is actually loaded in Application_AcquireRequestState,
so that's another option. Again, these are all in global.asax.
 
S

Stu

Thanks - that worked great.

Stu

Brock Allen said:
Session state isn't loaded until later in the lifetime of the request.
Instead of Application_BeginRequest, build
Application_PreRequestHandlerExecute --
this is the safest place to do your work prior to the page getting called.
The step where session is actually loaded in
Application_AcquireRequestState, so that's another option. Again, these
are all in global.asax.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top