ASP.NET MVC是否会使浏览器刷新使TempData无效? [英] ASP.NET MVC does browser refresh make TempData useless?

查看:62
本文介绍了ASP.NET MVC是否会使浏览器刷新使TempData无效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

If I redirect to a new page passing TempData to initialise the page it works fine, however if the user presses the refresh button in their browser the TempData is no-longer available. Given this, is there any situation where TempData could be used reliably?
Or any way to remove or mitigate the problem of users refreshing?

推荐答案

在MVC 1中,是的,在存储密钥之后的下一个请求之后,临时数据会丢失.

In MVC 1, yes, temp data is lost after the next request after you store a key.

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

With MVC 2 however, the temp data is lost after the first attempt to access it.

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

You can always use Session, which TempData uses anyway, to solve the temp data loss issue your having.

摘自《 MVC 2 Beta发行说明》:

TempDataDictionary的改进

TempDataDictionary Improvements

TempDataDictionary的行为 班级已稍微更改为 解决临时数据在其中的情况 要么过早地删除,要么 持续超过必要的时间.为了 例如,在临时数据为 在相同的请求中阅读 设置好后,温度数据就持续存在 对于下一个请求,即使 目的是将其删除.其他 情况下,临时数据未持久 跨多个连续重定向.

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.

为解决这些情况, TempDataDictionary类已更改 这样所有的钥匙都可以幸存 无限期地直到读取密钥 从TempDataDictionary对象. Keep方法已添加到 TempDataDictionary让您指出 该值不应删除 看完之后.这 RedirectToActionResult是一个示例 在其中调用Keep方法的位置 为了保留所有密钥 下一个请求.

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源代码中查看这些更改:

You can also look directly in the MVC 2 source to see these changes:

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是否会使浏览器刷新使TempData无效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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