在ASP中,会话断断续续地丢失它.asp.net MVC 5

本文关键字:asp net MVC ASP 会话 断断续续 | 更新日期: 2023-09-27 18:14:10

您是否看到会话间歇性地失去其值的原因?

    public ActionResult Index(string app) // this view hits the Create Controller on a form post event
    {
       Session["appType"] = app;         
       return View();
    }
    [HttpPost]
    public ActionResult Create(string userName, string password)
    {
        string a = Session["appType"].ToString(); // throws object reference error.
    }

在ASP中,会话断断续续地丢失它.asp.net MVC 5

    检查web.config中的sessionState元素。您将在超时(以分钟为单位)不活动后失去会话。

您将看到如下代码:

<sessionState mode="SQLServer"
    cookieless="true "
    regenerateExpiredSessionId="true "
    timeout="30"
    sqlConnectionString="Data Source=MySqlServer;Integrated Security=SSPI;"
    stateNetworkTimeout="30"/>
  • 你可以在负载平衡器后面运行这个应用程序吗?如果是这样,它可能不会在会话期间将您的用户路由到同一台机器。