check session key is exist ?

Jak
Jak
Member
364 Points
152 Posts

Hi,

i using mvc5.

how i can check session key is set or not (exist) ?

i do this 

if(Session["MyKey"]!=Null)
{
// do something
}

but i get error : 

Object reference not set to an instance of an object.

Views: 8238
Total Answered: 1
Total Marked As Answer: 1
Posted On: 07-Sep-2014 02:20

Share:   fb twitter linkedin
Answers
Rahul Maurya
Rahul M...
Moderator
1172 Points
211 Posts
         

Hi,

try this one:

if(Session["mykey"]!=null && Session["mykey"] !="")
{
 
//do my logic
}
else
{
 
//id session["mykey"] is not set redirect
 
RedirectToAction("Index");
}

Posted On: 07-Sep-2014 02:21
 Log In to Chat