[英]Session values returning null
我在本地(.Net 4.5)运行ASP MVC站点,尝试从会话中检索值时遇到问题。 我正在从类似于以下内容的静态帮助器类中调用代码:
Helpers.cs
public static void SetSessionValue(string key, object value)
{
HttpContext.Current.Session[key] = value;
}
public static object GetSessionValue(string key)
{
return HttpContext.Current.Session[key];
}
AjaxController.cs
public ActionResult SetUserName(string username)
{
Helpers.SetSessionValue("username", username);
}
public ActionResult GetUserName()
{
var username = Helpers.GetSessionValue("username");
return Json(new { valid = true, username });
}
上面的用户名是一个示例,但是我有多种情况发生,并且每次键的值为空,但键仍然存在。 我进一步将以下内容添加到SetSessionValue中进行测试:
public static void SetSessionValue(string key, object value)
{
HttpContext.Current.Session[key] = value;
var test = HttpContext.Current.Session[key];
}
变量测试将使用该值填充。 我仔细检查了所有变量名。 键仍然存在于集合中,但是值消失。
尝试的解决方案
解决方案1- HttpContext.Current.Session空项目
我在网络配置中设置了以下内容,不走运
<sessionState mode="InProc" timeout="20" />
解决方案2- 路由请求时HttpContext.Current.Session为null
我在网络配置中设置了以下内容,不走运
<remove name="Session" />
<add name="Session" type="System.Web.SessionState.SessionStateModule"/>
我尝试过的其他物品
<httpCookies requireSSL="true" />
不在web.config中 Session.Abandon();
或Session.Clear();
[WebMethod(EnableSession = true)]
您几乎检查了所有内容,仅使用无cookie模式检查,看看结果如何,我想您的cookie东西有问题:
<sessionState mode="InProc" cookieless="true" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.