TempData 和 ViewData 很快就会消失
有没有办法可以长时间存储变量?我的用户将让他们的窗口长时间打开(最多几 (3) 小时)。如果他们这样做,那么这些值就有可能变为空。如果正确的话,一旦加载视图,ViewData
就会变为 null。我会怎么做?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最好的选择是使用会话状态。或者,如果您的用户以某种方式进行身份验证,请将与帐户相关的信息保留在数据库中的某个位置。
如果您想使用会话,您可以在控制器的操作方法中使用类似的内容:
现在,会话状态最终也会消失,例如当用户关闭浏览器或处于非活动状态一段时间时。默认值为 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:
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 thesessionState
configuration element of your web.config.