C# 无法检查会话是否存在?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/759795/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 00:08:55  来源:igfitidea点击:

C# Cannot check Session exists?

c#asp.net-mvcsessionexists

提问by Martijn

I get an error when I do the following:

执行以下操作时出现错误:

if(Session["value"] != null)
{
   // code
}

The error i get is this:

我得到的错误是这样的:

Object reference not set to an instance of an object.

你调用的对象是空的。

Why is this? I always check my session this way? I am using the MVC Framework, does this has something to do with it?

为什么是这样?我总是以这种方式检查我的会话?我正在使用 MVC 框架,这与它有关吗?

EDIT:

编辑:

The code is in the constructor of a Controller:

代码在控制器的构造函数中:

public class MyController : ControllerBase
{
    private int mVar;

    public MyController()
    {
        if (Session["value"] != null)
        {
            mVar= (int)Session["value"];
        }
    }
}

采纳答案by Marc Gravell

The session only really exists during the processingof an action - I wouldn't expect it to be valid in the constructor of a controller. For example, the controller might (for all I know) be re-used between requests.

会话仅在处理操作期间才真正存在- 我不希望它在控制器的构造函数中有效。例如,控制器可能(据我所知)在请求之间重复使用。

You will need to do this either in the action (method), or (perhaps more appropriately) in an action filter, or the OnActionExecuting(etc) method(s):

您需要在操作(方法)或(可能更合适)操作过滤器或OnActionExecuting(等)方法中执行此操作:

public abstract class ControllerBase : Controller
{
    protected override void OnActionExecuting(
        ActionExecutingContext filterContext)
    {
        // code involving this.Session // edited to simplify
        base.OnActionExecuting(filterContext); // re-added in edit
    }
}

回答by Kirtan

You'll have to do it like this-

你必须这样做 -

if(null != HttpContext.Current.Session["Value"])
{
    //Code here.
}

回答by Graham Clark

You're getting this error because the "Session" object is null. Therefore it's impossible to look at the ["value"] bit of it. I'm not familiar with MVC, but either there's a bigger problem whereby the Session object isn't being created, or you're accessing it at the wrong point of the lifecycle, or you just need a test to check if Session != null.

您收到此错误是因为“会话”对象为空。因此不可能查看它的 ["value"] 位。我不熟悉 MVC,但要么存在一个更大的问题,即未创建 Session 对象,要么您在生命周期的错误点访问它,或者您只需要测试以检查 Session !=空值。

回答by Nick Whaley

The [] is an indexer, it acts like a method on the class.

[] 是一个索引器,它就像类上的一个方法。

In this case, Session is null and you cannot perform the indexing on it.

在这种情况下, Session 为空,您无法对其执行索引。

Do this:

做这个:

if(Session != null && Session["value"] != null)
{
   // code
}

回答by Cerebrus

The syntax used by you:

您使用的语法:

if (Session["mySessionVariable"] != null)
{

}

... is the correct way to check for a Session object in ASP.NET. I suspect that the problem is because you are using ASP.NET MVC.

... 是在 ASP.NET 中检查 Session 对象的正确方法。我怀疑问题是因为您使用的是 ASP.NET MVC。

Maybe some of our MVC experts can enlighten us as to the correct way of doing this in ASP.NET MVC.

也许我们的一些 MVC 专家可以启发我们在 ASP.NET MVC 中执行此操作的正确方法。

Edit:I see that Marc Gravell has posted his answer while I'm typing this. That should be illuminatory.

编辑:我看到 Marc Gravell 在我打字时发布了他的答案。那应该是有启发性的。

回答by Wilson Fernandes

I solve by this way:

我是这样解决的:

if (Session.Count > 0 && Session["mySessionVariable"].ToString() != null)
{

}

回答by Behnam Mohammadi

if(Session != null && Session["name"] != null && Session["name"].ToString()!="")
{
   //fire code
}

回答by Pablo Rocha Villagra

You also can use:

您还可以使用:

if (Session == null || String.IsNullOrEmpty((string)Session["session_object"])){
   // Do something
}