gpt4 book ai didi

asp.net-mvc - ASP.NET MVC是否会使浏览器刷新使TempData无效?

转载 作者:行者123 更新时间:2023-12-04 08:58:33 25 4
gpt4 key购买 nike

如果我重定向到通过TempData的新页面以初始化页面,则它可以正常工作,但是,如果用户在浏览器中按下刷新按钮,则TempData将不再可用。
鉴于此,是否存在可以可靠使用TempData的情况?
还是有任何消除或减轻用户刷新问题的方法?

最佳答案

在MVC 1中,是的,在存储 key 之后的下一个请求之后,临时数据将丢失。

但是,对于MVC 2,临时数据在首次尝试访问之后会丢失。

您始终可以使用Session(无论如何,TempData都会使用它)来解决您遇到的临时数据丢失问题。

来自MVC 2 Beta发行说明:

TempDataDictionary Improvements

The behavior of the TempDataDictionary class has been changed slightly to address scenarios where temp data was either removed prematurely or persisted longer than necessary. For example, in cases where temp data was read in the same request in which it was set, the temp data was persisting for the next request even though the intent was to remove it. In other cases, temp data was not persisted across multiple consecutive redirects.

To address these scenarios, the TempDataDictionary class was changed so that all the keys survive indefinitely until the key is read from the TempDataDictionary object. The Keep method was added to TempDataDictionary to let you indicate that the value should not be removed after reading. The RedirectToActionResult is an example where the Keep method is called in order to retain all the keys for the next request.



您还可以直接在MVC 2源代码中查看以查看这些更改:

MVC 1:
  public object this[string key] {
get {
object value;
if (TryGetValue(key, out value)) {
return value;
}
return null;
}
set {
_data[key] = value;
_modifiedKeys.Add(key);
}
}

MVC 2:
   public object this[string key] {
get {
object value;
if (TryGetValue(key, out value)) {
_initialKeys.Remove(key);
return value;
}
return null;
}
set {
_data[key] = value;
_initialKeys.Add(key);
}
}

关于asp.net-mvc - ASP.NET MVC是否会使浏览器刷新使TempData无效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2642062/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com