在什么情况下该HttpContext.Current.Session可以为空? [英] In what situations the HttpContext.Current.Session can be null?

查看:404
本文介绍了在什么情况下该HttpContext.Current.Session可以为空?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在什么情况下该HttpContext.Current.Session可以为空?

In what situations the HttpContext.Current.Session can be null?

我有一些asp.net页面。

I'm having some asp.net pages.

我只是想知道,为什么我应该检查是否为NULL会话对象始终?

I'm just wondering why I should be checking the Session object against null always?

例如。

public static object GetSession(string key)
{

if (HttpContext.Current.Session != null)
{

return HttpContext.Current.Session[key];

}

return null;

}

非常感谢,

推荐答案

查看此页面:<一href="http://stackoverflow.com/questions/1382791/asp-net-what-to-do-if-current-session-is-null">asp.net怎么做,如果当前的会话是空?。它有的可能性pretty的很好的解释。

Check out this page: asp.net What to do if current session is null?. It's got a pretty good explanation of the possibilities.

修改

很少,你会是在一个情况下它是未知的,如果Session对象可用的你想从它访问的值。在大多数情况下,包括那些由其他的答案中提到, HttpContext.Current.Session [关键] 的将是空的,但不是的 HttpContext.Current.Session 的本身。

Rarely would you be a in a situation where it's unknown if the Session object is available and you want to access a value from it. In most instances, including those mentioned by other answers, HttpContext.Current.Session[key] will be null, but not HttpContext.Current.Session itself.

在最日常的编码方案中的Session对象将不能为空,而code在你的问题将是矫枉过正。同样,如果你知道这个Session对象是时间空未来,你甚至不应该试图访问它。

In most everyday coding scenarios the Session object will not be null, and the code in your question would be overkill. Likewise, if you know the Session object is null ahead of time, you should not even be attempting to access it.

如果您的应用程序正在访问Session对象在不同寻常的情况下时,它可能会或可能不会为null,则你的code将是一个很好的方法来处理它,比如那些在上面提到的问题描述。

If your application is accessing the Session object in the unusual scenario when it may or may not be null then your code would be a good way to handle it, such as those described in the above referenced question.

这篇关于在什么情况下该HttpContext.Current.Session可以为空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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