C# Cannot check Session exists?

前端 未结 8 2016
遥遥无期
遥遥无期 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:51

    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.

    Do this:

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

提交回复
热议问题