如果页面空闲一段时间,ViewData 和 TempData 会自动清除。
默认超时时间太短。我们如何增加这两个集合的超时时间?
谢谢
没有直接的控制方法ViewData和TempData持续时间。
ViewData每次请求后清除。
TempData在会话期间保留 - 因此,如果您增加会话持续时间,您可以延长此数据可用的时间。
// Expanded Answer 看来您正试图使用它TempData来存储或持久化信息。虽然TempData只要会话就存在;默认情况下,它会在下一次请求时被擦除。可以覆盖此功能并继续使用,TempData但这不是您应该做的。
如果您需要为用户持久化数据;那么你真的应该使用Session对象。
这很简单地使用如下
string somethingToStore = "This value is to be stored in session";
// Store value to session; it will now persist for the duration of the session
Session["SomeKey"] = somethingToStore;
// To access the stored value
string somethingVal = Session["SomeKey"] as string;