Asp – accessing SessionState in Global.Application_Error

asp.netglobal-asaxsession-state

In the Application_Error method in Global.asax I am trying to retrieve a value from session state.

I am able to access session state as long as I throw the exception. EG:

thow new Exception("Test exception");

However if it is an unhandled exception, i get the following error when trying to access session state: "Session state is not available in this context.".

Why the differences in behavior, is there a work around?

Thanks.

Best Answer

I hate ASP.NET sometimes...

So generating an error using:

Response.Redirect("thispagedoesnotexist.aspx", false); 

The above line will redirect to Application_Error with session state not available

However

throw new Exception("test");

The above line will redirect to Application_Error with session state AVAILABLE

So instead of doing this all in Application_Error, in one spot, I will have to use try/catches through out my code to catch errors. Then gather data from session, log and email error details, then finally redirect to friendly error page. Lots of extra code..

Conclusion: Application_Error is worthless.