How to Set and Retrieve Session in ASP.NET MVC 5?

南笙酒味 提交于 2019-12-25 01:02:38

问题


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


回答1:


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)


来源:https://stackoverflow.com/questions/45601674/how-to-set-and-retrieve-session-in-asp-net-mvc-5

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!