C# Cannot check Session exists?

前端 未结 8 2029
遥遥无期
遥遥无期 2020-12-14 20:26

I get an error when I do the following:

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

The error i get is this:

Object reference not

8条回答
  •  误落风尘
    2020-12-14 20:49

    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.

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

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

提交回复
热议问题