18

i want to check if the Session contains some key/value data, in my global.asax. I'm not sure when the earliest possible time (and method name) is, to check this.

thanks :)

Pure.Krome
  • 84,693
  • 113
  • 396
  • 647
  • Related/sort of a duplicate of: http://stackoverflow.com/questions/5977285/set-session-variable-in-application-beginrequest – Chris Moschini Mar 01 '13 at 05:54

4 Answers4

20

I always believed Application_AcquireRequestState was the first event in Global.asax that could access the current session. It's definitely not Application_BeginRequest.

ddc0660
  • 4,022
  • 2
  • 21
  • 30
  • This does in fact make Session available in MVC3 and 4 and IIS7 and 8, IF the request fits the ASP.Net pipeline; for example if the request is for a Controller or WebPage, Session is available. However if it's for an image, css file, etc, in most configurations Session is not available and doesn't even return null - just attempting to access it gets you an HttpException. – Chris Moschini Mar 01 '13 at 05:56
  • 2
    I know this answer is very old, but i searched for the same answer. i found some additional information: Application_AcquireRequestState is the right place to get the session before the request is executed by the handler. if you want to know, if a session is available then check Context.Session for null. this will not raise the exception and after you know, there is a session you can use the variable Session to access it. – zreptil Jan 10 '14 at 14:11
  • I ran into a problem where in Application_AcquireRequestState Session was still null. A session is only started when the Handler for the request implements IRequiresSessionState. If you can not change the handler but still need a session you can call 'Context.SetSessionStateBehavior(SessionStateBehavior.Required);' in the Application_BeginRequest to force the generation of a session. – wasigh Sep 11 '19 at 10:16
3

MSDN casually mentions that the session state is acquired during Application_PostAcquireRequestState event. I wish it was restated at the Life Cycle Overview page.

The latest you can access session state is in Application_PostRequestHandlerExecute, as it is saved by SessionStateModule during the next event Application_ReleaseRequestState.

Alexander Abramov
  • 1,470
  • 14
  • 20
  • AcquireRequestState also has it available; using PostAcquireRequestState doesn't prevent Session from throwing an exception for requests for images, css files, etc. – Chris Moschini Mar 01 '13 at 05:57
-3

You need to use BeginRequest (http://msdn.microsoft.com/en-us/library/system.web.httpapplication.beginrequest.aspx) as it is the first event fired on the HttpApplication object (which the Global.asax inherits).

You'll see more about the ASP.NET Application Lifecycle here - http://msdn.microsoft.com/en-us/library/ms178473.aspx (this is for IIS 5 & IIS 6).

Aaron Powell
  • 24,927
  • 18
  • 98
  • 150
  • 2
    It might be the first _event_ fired on the HttpApplication object, but has the session be deserialised, by then? – Pure.Krome Apr 19 '09 at 09:08
  • According to the lifecycle the HttpContext has been created, which HttpSessionState is a property of – Aaron Powell Apr 19 '09 at 11:35
  • 3
    apparently this Answer is incorrect according to http://stackoverflow.com/questions/1704940/when-is-the-earliest-i-can-access-session-in-the-asp-net-mvc-page-lifecycle – andy Nov 10 '09 at 01:11
  • 1
    This answer does not work. Use the one provided by @ddc0660 instead: http://stackoverflow.com/questions/765054/whens-the-earliest-i-can-access-some-session-data-in-global-asax/1710257#1710257 – Gan Oct 10 '11 at 05:34
-4

According to link text, the earliest events in global.asax that you can access session objects is when global.asax fires Session_Start event

Session__Start: Fired when a new user visits the application Web site.
Session__End: Fired when a user's session times out, ends, or they leave the application Web site

Community
  • 1
  • 1
Funky81
  • 1,679
  • 4
  • 18
  • 24