我应该怎么做,如果当前ASP.NET会话为空? [英] What should I do if the current ASP.NET session is null?

查看:102
本文介绍了我应该怎么做,如果当前ASP.NET会话为空?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Web应用程序,我做这样的事情来读取会话变量:

 如果(HttpContext.Current.Session = NULL&放大器;!&安培;!HttpContext.Current.Session [MyVariable的] = NULL)
{
    字符串MYVARIABLE =(字符串)HttpContext.Current.Session [MyVariable的];
}

我明白为什么要检查是很重要的,为什么HttpContext.Current.Session [MyVariable的]为空(变量可能没有被存储在Session尚未或会话已被重置因为各种原因),但是为什么我需要检查 HttpContext.Current.Session 为空?

我的理解是,本次会议是由ASP.NET自动创建的,因此绝不HttpContext.Current.Session应该为null。这是假设是正确的?如果它可以为空,这是否意味着我也应该在它储存的东西之前检查一下:

 如果(HttpContext.Current.Session!= NULL)
{
    HttpContext.Current.Session [MyVariable的] =测试;
}
其他
{
    //我应该在此情况下(如会话为null)做些什么呢?
    //是否有可能迫使会话中创建,如果它不存在?
}


解决方案

是的,Session对象可能为空,但只在某些情况下,您将很少遇到:


  • 如果您已禁用<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.sessionstate.sessionstatemodule.aspx\">SessionState HTTP模块,完全禁用会话

  • 如果您的code之前运行<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.httpapplication.acquirerequeststate.aspx\">HttpApplication.AcquireRequestState事件。

  • 您$​​ C $ C在的IHttpHandler 运行,一点不指定了IRequiresSessionState或<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.sessionstate.ireadonlysessionstate.aspx\">IReadOnlySessionState接口。

如果你只有在页面code,你就不会遇到这个。我的大多数ASP .NET code的使用会话,而不用重复检查空。它,然而,值得思考,如果你正在开发一个IHttpModule的或以其他方式是倒在ASP .NET的粗砂细节。

修改

在回答评论:不管是不是会话状态可要看的AcquireRequestState事件是否已经运行的请求。这是会话状态的模块会通过读取会话cookie和寻找此时,相应设置会话变量为你的工作。

的AcquireRequestState运行控制交给你的页面之前。所以,如果你调用其他功能,包括静态类,从您的网页,你应该罚款。

如果你有一些类启动过程中做初始化逻辑,例如在Application_Start事件或使用静态构造函数,会话状态可能无法使用。这一切都归结到是否有当前请求和的AcquireRequestState已运行。

此外,应在客户端禁用了Cookie,Session对象将仍然可用 - 但下一个请求,用户将有一个新的空会话返回。这是因为客户端被赋予一个会话StateBag的,如果他不已经有一个。如果客户端不运会话cookie,我们没有识别客户端一样的方式,所以他会一次又一次地递给一个新的会话。

In my web application, I do something like this to read the session variables:

if (HttpContext.Current.Session != null &&  HttpContext.Current.Session["MyVariable"] != null)
{
    string myVariable= (string)HttpContext.Current.Session["MyVariable"];
}

I understand why it's important to check why HttpContext.Current.Session["MyVariable"] is null (the variable might not have been stored in the Session yet or the Session has been reset for various reasons), but why do I need to check if HttpContext.Current.Session is null?

My understanding is that the session is created automatically by ASP.NET therefore HttpContext.Current.Session should never be null. Is this assumption correct? If it can be null, does it mean I should also check it before storing something in it:

if (HttpContext.Current.Session != null)
{
    HttpContext.Current.Session["MyVariable"]="Test";
}
else
{
    // What should be done in this case (if session is null)?
    // Is it possible to force the session to be created if it doesn't exist?
}

解决方案

Yes, the Session object might be null, but only in certain circumstances, which you will only rarely run into:

If you only have code in pages, you won't run into this. Most of my ASP .NET code uses Session without checking for null repeatedly. It is, however, something to think about if you are developing an IHttpModule or otherwise is down in the grittier details of ASP .NET.

Edit

In answer to the comment: Whether or not session state is available depends on whether the AcquireRequestState event has run for the request. This is where the session state module does it's work by reading the session cookie and finding the appropiate set of session variables for you.

AcquireRequestState runs before control is handed to your Page. So if you are calling other functionality, including static classes, from your page, you should be fine.

If you have some classes doing initialization logic during startup, for example on the Application_Start event or by using a static constructor, Session state might not be available. It all boils down to whether there is a current request and AcquireRequestState has been run.

Also, should the client have disabled cookies, the Session object will still be available - but on the next request, the user will return with a new empty Session. This is because the client is given a Session statebag if he does not have one already. If the client does not transport the session cookie, we have no way of identifying the client as the same, so he will be handed a new session again and again.

这篇关于我应该怎么做,如果当前ASP.NET会话为空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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