第二个请求后的TempData不会破坏 [英] TempData won't destroy after second request

查看:146
本文介绍了第二个请求后的TempData不会破坏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我把一个值TempData的第一次请求的actionfilter。

  filterContext.Controller.TempData [价值] =真;

之后的第二个请求进来,我检查值

  filterContext.Controller.TempData.ContainsKey(值)

价值是存在的。随后的第三请求进来,我再次检查值

  filterContext.Controller.TempData.ContainsKey(值)

和值仍是present。不应该是第二个请求后销毁这个值?所有的请求都AJAX请求。


解决方案

  

不应该被这种价值的第二个请求后销毁?


只有当你读它:

  VAR值= filterContext.Controller.TempData [值];

如果你不读从TempData的它不会被驱逐的价值。

这里的 TempData.Items 的getter是如何定义的:

 公共对象get_Item(字符串键)
{
    反对OBJ2;
    如果(this.TryGetValue(键,出OBJ2))
    {
        this._initialKeys.Remove(键);
        返回OBJ2;
    }
    返回null;
}

注意如何只有当你调用getter和仅当值的集合中被发现的价值将被驱逐。在code你给你要做的就是检查是否TempData的包含给定关键,但你没有看过这个键的值。

如果你愿意,你可以手动驱逐TempData的值:

  filterContext.Controller.TempData.Remove(值);

和也有它允许你不删除它读值的方法:

  VAR值= filterContext.Controller.TempData.Peek(值);

I'm putting a value into TempData on first request in an actionfilter.

filterContext.Controller.TempData["value"] = true;

after that a second request comes in and I check for the value

filterContext.Controller.TempData.ContainsKey("value")

the value is there. Then a third request comes in and I check for the value again

filterContext.Controller.TempData.ContainsKey("value")

and the value is still present. Shouldn't be this value destroyed after the second request ? All request are AJAX requests.

解决方案

Shouldn't be this value destroyed after the second request ?

Only if you read it:

var value = filterContext.Controller.TempData["value"];

If you don't read the value from the TempData it won't be evicted.

Here's how the TempData.Items getter is defined:

public object get_Item(string key)
{
    object obj2;
    if (this.TryGetValue(key, out obj2))
    {
        this._initialKeys.Remove(key);
        return obj2;
    }
    return null;
}

Notice how the value will be evicted only if you call the getter and only if the value was found in the collection. In the code you have shown all you do is check whether the TempData contains a given key but you haven't read the value of this key.

You could manually evict the TempData value if you want:

filterContext.Controller.TempData.Remove("value");

And there's also a method which allows you to read the value without removing it:

var value = filterContext.Controller.TempData.Peek("value");

这篇关于第二个请求后的TempData不会破坏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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