为什么我失去我的会话变量? [英] Why I lose my session variable?

查看:128
本文介绍了为什么我失去我的会话变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I'm对MVC和使用KnockoputJS。我选择2选择值。在第一个选择我选择IDCompany,第二个选择我选择IDSubsidiary。

I´m on MVC and using KnockoputJS. I select the value from 2 select. In the first select i choose IDCompany, and the second select i choose IDSubsidiary.

我送模型JSON来JsonResult控制器和我创建一个变量会话和cookie,并保存IDCompany在会话变量和具有相同名称的Cookie。

I send the model in Json to JsonResult in the controller and I create a variable session and a cookie and save IDCompany in the Session variable and in the cookie with the same name.

我做IDSubsidiary相同。最后,我回到AJAX功能(它称之为保存先)

I do the same with IDSubsidiary. Finally, I return to the ajax function (which call "Save" at first)

[HttpPost]
public JsonResult Save(ViewModel viewModel)
{
  Session["IDCompany"] = viewModel.IDCompany.ToString();
  Response.Cookies["IDCompany"].Value = viewModel.IDCompany.ToString();
  Response.Cookies["IDCompany"].Expires = DateTime.Now.AddDays(1);

  Session["IDSubsidiary"] = viewModel.IDSubsidiary.ToString();
  Response.Cookies["IDSubsidiary"].Value = viewModel.IDSubsidiary.ToString();
  Response.Cookies["IDSubsidiary"].Expires = DateTime.Now.AddDays(1);

  return Json(true);
}

问题是,经过一段时间(30分钟左右),我失去了会话[IDCompany] 会话[IDSubsidiary] (将变为null)。
这个问题可能是,例如,会话[IDSubsidiary]和 Response.Cookies [IDSubsidiary] 具有相同的名称?

The problem is that after a while (30 mins approximately), i lose Session["IDCompany"] and Session["IDSubsidiary"] (becomes null). The problem can be that, for example, Session["IDSubsidiary"] and Response.Cookies["IDSubsidiary"] has the same name?

推荐答案

有两方面的原因,这可能发生。 1)会话超时,或2)使用的是进行中会话状态。

There are two reasons this could be happening. 1) The session is timing out, or 2) you are using "In Process" session state.

如果用户在网页上的三十分钟,然后将值消失了,他们刷新或转到另一个页面,它可能是超时问题的下一次。你可以尝试增加的sessionState超时;但是,你可能会开始运行到下面描述的问题。如果你有决心用Session变量,你应该切换到不同的状态模式不是过程,这是默认的。

If the user sits on a page for thirty minutes, and then the value is gone the next time they refresh or go to another page, its likely a timeout problem. You could try increasing the sessionState timeout; however, you'll probably start running into the issue described below. If you are determined to use Session variables, you should probably switch to a different state mode than "in process" which is the default.

如果它不超时,你的价值丢失的原因是因为进行中会话状态消失时,应用程序池回收。这种情况可能有多种原因。你可能想改变你的会话状态模式状态服务器或SQL Server。这将让您的会话数据之间围绕应用程序池回收,但您需要在Web服务器上启用ASP.NET会话状态服务,如果你去的国家服务器的路由。

If it is not timing out, the reason your value is lost is because "In Process" session state, goes away when the App Pool recycles. This can happen for a variety of reasons. You probably want to change your session state mode to State Server or SQL Server. This will keep your session data around between app pool recycles, but you will need to enable the "ASP.NET Session State Service" on the web server if you go the State Server route.

有几个州的模式,每种模式不同的行为。你可以阅读他们这里MSDN上。

There are several state modes, each with different behaviors. You can read about them here on MSDN.

这篇关于为什么我失去我的会话变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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