会话不在请求之间保持不变

时间:2013-12-02 16:56:19

标签: c# asp.net-mvc

在我查看MVC4 Session not persisted between requests和其他各个人时创建此内容,但这些都没有帮助解决我的问题。

我的MVC控制器上有以下两种操作方法:

    public ActionResult Capture(string testValue)
    {
        Session["Test"] = testValue;
        ViewBag.Test = Session["Test"]; // This works and correctly sets the value
        return View();
    }

    [HttpPost]
    public ActionResult Capture(SomeViewModel viewModel)
    {
        ViewBag.Test = Session["Test"]; // At this point the value in the session is inexplicably null, and thus so is the item in the ViewBag

        return View(viewModel);
    }

当我尝试在我的视图中使用ViewBag.Test

  • 在最初的Get上,它可以正常工作
  • 在帖子上,它为空。

如果我在代码中设置断点,会话对象已设置并且在初始“Get”中具有正确的值但在Post之后为null,则行为相同。

我的会话发生了什么,为什么请求之间的值不存在?

我已经确定会话是在每个请求上创建的,但是看不出原因。

1 个答案:

答案 0 :(得分:2)

由于这是用于安全应用程序,我在web.config中有以下设置: <httpCookies httpOnlyCookies="true" requireSSL="true" />

但是,我的本地环境不是通过HTTPS运行的。这导致在每个请求上重新创建.net会话,因为无法通过非安全连接发出cookie。从核心web.config中删除此行并将其添加回web.release.config转换解决了该问题。

相关问题