I want to read session id in application error event but always get error "Session state is not available in this context". Why? The strange thing is that I have the same code in another asp.net app and everything works fine.
我想在应用程序错误事件中读取会话ID,但始终会收到错误“在此上下文中无法使用会话状态”。为什么?奇怪的是,我在另一个asp.net应用程序中有相同的代码,一切正常。
void Application_Error(object sender, EventArgs e)
{
var sessionId = Session.SessionID;
//skipped code
}
3 个解决方案
#1
29
The session object may not be available this is dependent on when the error occured.
会话对象可能不可用,这取决于发生错误的时间。
For example if an error occured on Begin_Request the session would not be available as it has not yet been created.
例如,如果在Begin_Request上发生错误,则会话将不可用,因为它尚未创建。
So in summary sometimes it will work sometimes not, depending on when the error occured.
因此总的来说,有时候它根本不起作用,具体取决于发生错误的时间。
Best to check the state of the session object before accesssing the session id e.g.
最好在访问会话ID之前检查会话对象的状态,例如
HttpContext context = HttpContext.Current;
if (context != null && context.Session != null) ...
#2
2
check if any event missing in c# which is mapped to a control or issue in design part
检查c#中是否缺少任何映射到设计部分中的控件或问题的事件
#3
1
Application_error can fire in situations where a session is not present, for example when the garbage collector cleans up. The source of the error may not have been a user thread.
Application_error可以在会话不存在的情况下触发,例如垃圾收集器清理时。错误的来源可能不是用户线程。
Just check whether session is null first.
只需检查会话是否为空。
Simon
西蒙
#1
29
The session object may not be available this is dependent on when the error occured.
会话对象可能不可用,这取决于发生错误的时间。
For example if an error occured on Begin_Request the session would not be available as it has not yet been created.
例如,如果在Begin_Request上发生错误,则会话将不可用,因为它尚未创建。
So in summary sometimes it will work sometimes not, depending on when the error occured.
因此总的来说,有时候它根本不起作用,具体取决于发生错误的时间。
Best to check the state of the session object before accesssing the session id e.g.
最好在访问会话ID之前检查会话对象的状态,例如
HttpContext context = HttpContext.Current;
if (context != null && context.Session != null) ...
#2
2
check if any event missing in c# which is mapped to a control or issue in design part
检查c#中是否缺少任何映射到设计部分中的控件或问题的事件
#3
1
Application_error can fire in situations where a session is not present, for example when the garbage collector cleans up. The source of the error may not have been a user thread.
Application_error可以在会话不存在的情况下触发,例如垃圾收集器清理时。错误的来源可能不是用户线程。
Just check whether session is null first.
只需检查会话是否为空。
Simon
西蒙