0

I am working on an ASP.NET MVC 5 application and I am having problem storing data to session. The value I get is always null.

Here is where I set the session:

string mail = user.Email;
string response = user.CheckEmail();
Session["email"] = mail;

I am testing the session here, It is redirecting to YYY:

if ((string)Session["mail"] != null)
{
    return RedirectToAction("PPP");
}
else
{
    return RedirectToAction("YYY");
}

Please immediate help will be appreciated. Thanks

5
  • I just build the project after coding. Cant really pinpoint the cause Commented Aug 9, 2017 at 22:44
  • What session service are you using? Are you certain that the client is retaining a session token? Commented Aug 9, 2017 at 22:46
  • Possible duplicate of What is the right time for ViewData, ViewBag, Session, TempData Commented Aug 9, 2017 at 22:50
  • You are saving the session email, but looking later for mail. The two don't match. Commented Aug 9, 2017 at 23:18
  • Thanks for the support. I just noticed the error. Much love Commented Aug 10, 2017 at 7:00

1 Answer 1

8

You have typo.

Session["email"] = mail;

if ((string)Session["mail"] != null)
                    ^^^^

Session name should be email. In addition, you should not cast to string in order to check null.

if (Session["email"] != null)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.