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

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

问题描述

在我的 Web 应用程序中,我执行以下操作来读取会话变量:

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"];
}

我理解为什么检查 HttpContext.Current.Session["MyVariable"] 为 null 的原因很重要(该变量可能尚未存储在 Session 中,或者由于各种原因该 Session 已被重置),但为什么我要检查需要检查 HttpContext.Current.Session 是否为空?

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?

我的理解是会话是由 ASP.NET 自动创建的,因此 HttpContext.Current.Session 永远不应该为空.这个假设正确吗?如果它可以为空,是否意味着我还应该在存储内容之前检查它:

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?
}

推荐答案

是的,Session 对象可能为 null,但仅限于某些情况下,您很少会遇到这种情况:

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

  • If you have disabled the SessionState http module, disabling sessions altogether
  • If your code runs before the HttpApplication.AcquireRequestState event.
  • Your code runs in an IHttpHandler, that does not specify either the IRequiresSessionState or IReadOnlySessionState interface.

如果您只有页面中的代码,您就不会遇到这种情况.我的大部分 ASP .NET 代码使用 Session 而不重复检查 null.但是,如果您正在开发 IHttpModule 或在 ASP .NET 的更详细的细节方面有所欠缺,则需要考虑一下.

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.

回复评论:会话状态是否可用取决于是否为请求运行了 AcquireRequestState 事件.这是会话状态模块通过读取会话 cookie 并为您找到合适的会话变量集来完成工作的地方.

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 在控制权交给您的主页之前运行.因此,如果您从页面调用其他功能,包括静态类,则应该没问题.

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.

如果您有一些类在启动期间执行初始化逻辑,例如在 Application_Start 事件上或通过使用静态构造函数,会话状态可能不可用.这一切都归结为当前是否有请求并且 AcquireRequestState 是否已运行.

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.

此外,如果客户端禁用了 cookie,Session 对象仍然可用 - 但在下一个请求时,用户将返回一个新的空 Session.这是因为如果客户端还没有会话状态包,则会为其提供一个会话状态包.如果客户端不传输会话cookie,我们就没有办法将客户端识别为相同的,所以他会一次又一次地被交给一个新的会话.

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天全站免登陆