问题描述
在Global.asax中Application_Error事件,我尝试从会话状态的值。
In the Application_Error method in Global.asax I am trying to retrieve a value from session state.
我可以,只要我抛出的异常访问会话状态。 EG:
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?
感谢。
推荐答案
我讨厌ASP.NET有时...
I hate ASP.NET sometimes...
所以使用产生一个错误:
So generating an error using:
Response.Redirect("thispagedoesnotexist.aspx", false);
上面一行将重定向一起的Application_Error会话状态不可用
The above line will redirect to Application_Error with session state not available
然而
throw new Exception("test");
上面一行将重定向一起的Application_Error会话状态AVAILABLE
The above line will redirect to Application_Error with session state AVAILABLE
因此,而不是做这一切的Application_Error的,在一个地方,我将不得不使用try /捕获通过我的code捕获错误。然后,从会话收集数据,日志和电子邮件的错误详细信息,然后终于重定向到友好的错误页面。额外的code大量..
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..
结论:的Application_Error是毫无价值的。
Conclusion: Application_Error is worthless.
这篇关于在Global.Application_Error访问SessionState会的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!