TempData 和 ViewData 很快就会消失

发布于 2024-12-17 05:03:47 字数 124 浏览 4 评论 0 原文

有没有办法可以长时间存储变量?我的用户将让他们的窗口长时间打开(最多几 (3) 小时)。如果他们这样做,那么这些值就有可能变为空。如果正确的话,一旦加载视图,ViewData 就会变为 null。我会怎么做?

Is there a way that I can store variables for long periods of time? My users will be leaving their windows open for extended periods of time (Up to a couple (3) of hours). If they do that, then there is a chance that the values will become null. If am correct, ViewData goes null as soon as the view is loaded. What would I do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情未る 2024-12-24 05:03:47

最好的选择是使用会话状态。或者,如果您的用户以某种方式进行身份验证,请将与帐户相关的信息保留在数据库中的某个位置。

如果您想使用会话,您可以在控制器的操作方法中使用类似的内容:

Session["Key"] = somevalue;

现在,会话状态最终也会消失,例如当用户关闭浏览器或处于非活动状态一段时间时。默认值为 20 分钟。如果要更改此设置,请修改 timeout 属性>web.config 的 sessionState 配置元素。

Your best bet would be to use Session State. Or, if your users are authenticating somehow, persist the information relative to the account in a database somewhere.

If you want to use session, you can use something like this in a controller's action method:

Session["Key"] = somevalue;

Now, session state dies eventually as well, such as when the user closes their browser, or has been inactive for a while. The default is 20 minutes. If you want to change this, modify the timeout attribute under the sessionState configuration element of your web.config.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文