加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – ASP.NET MVC做浏览器刷新让TempData无用吗?

发布时间:2020-12-16 04:30:05 所属栏目:asp.Net 来源:网络整理
导读:如果我重定向到通过TempData初始化页面的新页面,它可以正常工作,但是如果用户在浏览器中按下刷新按钮,则TempData不再可用. 鉴于此,是否存在TempData可靠使用的情况? 或者以任何方式删除或缓解用户刷新的问题? 解决方法 在MVC 1中,是的,在存储密钥之后,临时
如果我重定向到通过TempData初始化页面的新页面,它可以正常工作,但是如果用户在浏览器中按下刷新按钮,则TempData不再可用.
鉴于此,是否存在TempData可靠使用的情况?
或者以任何方式删除或缓解用户刷新的问题?

解决方法

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

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

您始终可以使用TempData使用的Session来解决您遇到的临时数据丢失问题.

来自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);
        }
    }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读