accessing SessionState in Global.Application_Error

瘦欲@ 提交于 2020-01-05 08:14:37

问题


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.


回答1:


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.




回答2:


I think you are trying to access the session through HttpContext.Current.Session. I believe the difference in behavior is that in the unhanded exception handler, the request has gone into "Failsafe" mode and the page lifecycle (including loading and disposing the session) has finished.

Check out this page on the lifecycle for more info



来源:https://stackoverflow.com/questions/300093/accessing-sessionstate-in-global-application-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!