会话状态是在此上下文中可用​​的 [英] Session state is not available in this context

查看:239
本文介绍了会话状态是在此上下文中可用​​的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想读应用程序错误事件的会话ID,但总是错误的会话状态不是在这种情况下可用。为什么?奇怪的是,我在另外一个asp.net应用程序在同一code和一切工作正常。

 无效的Application_Error(对象发件人,EventArgs的发送)
{        VAR的sessionId = Session.SessionID;
        //跳过code}


解决方案

会话对象可能不可用,这是依赖于时出错。

例如,如果上Begin_Request发生错误,因为它尚未创建的会话将无法使用。

因此​​,在总结,有时它会工作有时不,这取决于当错误发生。

最佳accesssing会话ID例如之前检查会话对象的状态。

  HttpContext的背景下= HttpContext.Current;如果(上下文= NULL&放大器;!&安培;!context.Session = NULL)...

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.

void Application_Error(object sender, EventArgs e)
{

        var sessionId = Session.SessionID;
        //skipped code

}

解决方案

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.

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.

HttpContext context = HttpContext.Current;

if (context != null && context.Session != null) ...

这篇关于会话状态是在此上下文中可用​​的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆