Accessing SessionState in Global.Application_Error

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

I can access the session state while I am throwing an exception. EG:

thow new Exception("Test exception");

      

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

Why differences in behavior, is there work around?

Thank.

+1


source to share


2 answers


I hate ASP.NET sometimes ...

So, create an error using:

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

      

The above line will be redirected to Application_Error with session state unavailable

However



throw new Exception("test");

      

The above line will be redirected to Application_Error with session state AVAILABLE

So instead of doing it all in Application_Error, in one place I'll have to use try / catch in my code to catch errors. Then collect data from session, log and email error details and then finally redirect to friendly error page. Lots of extra code ..

Conclusion: Application_Error is useless.

+2


source


I think you are trying to access the session via HttpContext.Current.Session. I believe the difference in behavior is that in the unhandled exception handler, the request went into "Failsafe" mode and the page life cycle (including loading and deleting the session) is complete.



Take a look at this lifecycle page for more information

0


source







All Articles