微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如果我重定向到通过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);
        }
    }

原文地址:https://www.jb51.cc/aspnet/248482.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐