C# 会话值返回null
我正在本地运行一个ASP MVC站点(.Net 4.5),在尝试从会话检索值时遇到了一个问题。我正在从静态助手调用代码 类别类似于以下内容: Helpers.csC# 会话值返回null,c#,asp.net-mvc,session-variables,C#,Asp.net Mvc,Session Variables,我正在本地运行一个ASP MVC站点(.Net 4.5),在尝试从会话检索值时遇到了一个问题。我正在从静态助手调用代码 类别类似于以下内容: Helpers.cs public static void SetSessionValue(string key, object value) { HttpContext.Current.Session[key] = value; } public static object GetSessionValue(string key)
public static void SetSessionValue(string key, object value)
{
HttpContext.Current.Session[key] = value;
}
public static object GetSessionValue(string key)
{
return HttpContext.Current.Session[key];
}
public ActionResult SetUserName(string username)
{
Helpers.SetSessionValue("username", username);
}
public ActionResult GetUserName()
{
var username = Helpers.GetSessionValue("username");
return Json(new { valid = true, username });
}
AjaxController.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];
}
public ActionResult SetUserName(string username)
{
Helpers.SetSessionValue("username", username);
}
public ActionResult GetUserName()
{
var username = Helpers.GetSessionValue("username");
return Json(new { valid = true, username });
}
上面的用户名就是一个例子,但我有很多这样的情况,每次密钥的值为null,但密钥仍然存在。我又去了
打开,并将以下内容添加到要测试的SetSessionValue中:
public static void SetSessionValue(string key, object value)
{
HttpContext.Current.Session[key] = value;
var test = HttpContext.Current.Session[key];
}
变量测试将填充该值。我仔细检查了所有变量名。键仍然存在于集合中,但值消失
尝试的解决方案
public static void SetSessionValue(string key, object value)
{
HttpContext.Current.Session[key] = value;
}
public static object GetSessionValue(string key)
{
return HttpContext.Current.Session[key];
}
public ActionResult SetUserName(string username)
{
Helpers.SetSessionValue("username", username);
}
public ActionResult GetUserName()
{
var username = Helpers.GetSessionValue("username");
return Json(new { valid = true, username });
}
解决方案1-
我在我的网络配置中设置了以下内容,运气不好
<sessionState mode="InProc" timeout="20" />
<remove name="Session" />
<add name="Session" type="System.Web.SessionState.SessionStateModule"/>
解决方案2-
我在我的网络配置中设置了以下内容,运气不好
<sessionState mode="InProc" timeout="20" />
<remove name="Session" />
<add name="Session" type="System.Web.SessionState.SessionStateModule"/>
我试过的其他项目
- 确保web.config中没有
- 尝试启用并运行SSL
- 在释放模式下运行
- 确保没有
会话。放弃()代码>或
会话.清除()代码>
- 确保没有运行病毒扫描
- 做了一次搜索以确保我没有在其他地方更新
- 确保我的ASP.NET状态服务正在运行
- 已尝试添加到
[WebMethod(EnableSession=true)]
- 我已经确保启用了cookies
<sessionState mode="InProc" cookieless="true" />
找到了问题。。。感觉自己像个白痴。尽管我去关闭了IIS Express和我正在运行的服务中的所有实例,但仍有另一个项目实例在某处运行,从而混淆了我的会话项 为了确保一切都是最新的,我进去修改了端口号 (右键单击项目->属性)
这样做之后,一切都如期进行。这可能是一个非常独特的情况,但希望发布,以防有人遇到类似情况。检查浏览器中是否启用了cookies只是再次检查,以确保它们是……您是否真的调用了第二个参数为null的SetSessionValue/SetUserName?可能在调用SetSessionValue时添加一些日志记录。我已经进行了详细说明以确保。在上面更新的SetSessionValue中加上测试。我确定我在传递一个值,它离开方法后似乎丢失了。你没有在GetUserName方法中设置username变量。请更新codeHaha,刚刚看到你的答案,它真的很复杂,但无论如何,doneCool,很高兴你发现了问题。无论如何,cookieless可能是一些浏览器的解决方案。