如何在ASP.NET MVC 5中设置和检索会话? [英] How to Set and Retrieve Session in ASP.NET MVC 5?

查看:85
本文介绍了如何在ASP.NET MVC 5中设置和检索会话?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ASP.NET MVC 5应用程序,但在将数据存储到会话时遇到问题.我得到的值始终为空.

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.

在这里设置会话:

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

我正在这里测试会话,它正在重定向到YYY:

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

推荐答案

您有错字.

Session["email"] = mail;

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

会话名称应为 电子邮件 .另外,您不应强制转换为 string 来检查 null .

if (Session["email"] != null)

这篇关于如何在ASP.NET MVC 5中设置和检索会话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆