Aug. 21, 2007, 3:37 p.m.
IT

ASP.NET, Global.asax, Application_Error and "lost" Session

I recently ran into a weird issue with ASP.NET. In my Global.asax file I have a handler for Application_Error to display a standard error page. However, in the Master Page (the handler transfers to an error page that inherits from a custom master page), I read the session state to show a global status.

This works fine, until an unhandled exception is thrown. Then I suddenly get an exception stating that Session is null. This made no sense, and I even discovered a response from someone on the internet that once an unhandled exception was thrown, the session state is reset (which is preposterous). Eventually I found the reason - the Application_Error event is raised *before* the AcquireRequestState event is raised - which sets up the Session state.

Simple conclusion - Session state is not available in Application_error.