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

前端之家收集整理的这篇文章主要介绍了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.f2er.com/aspnet/248482.html

猜你在找的asp.Net相关文章