TempData的坚持阅读ASP.NET MVC 2后 [英] TempData persisting after read in ASP.NET MVC 2

查看:140
本文介绍了TempData的坚持阅读ASP.NET MVC 2后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在ASP.NET MVC 2,的TempData 值,直到会话结束或直到它们被读取。在微软的 ...

In ASP.NET MVC 2, TempData values persist until the session ends or until they are read. In the words of Microsoft...

TempData的中值一直保持到
  它被读取或直到会话时间
  出。以这种方式持续的TempData
  使场景,如重定向,
  因为在TempData的值是
  可超越单一的请求。

The value of TempData persists until it is read or until the session times out. Persisting TempData in this way enables scenarios such as redirection, because the values in TempData are available beyond a single request.

我想我明白这一点,但我只是碰到不寻常的行为排在我的应用程序,其中一个的TempData 值可用,它不应该已经面世。一般情况下,我有一系列的动作控制器,其中的第一个动作设置一个的TempData 值,接下来的几个动作看,然后设置的TempData 价值,最后动作读取TempData的值。伪code低于...

I thought I understood this but I just came across unusual behavior in my application where a TempData value was available and it should not have been available. In general, I have a controller with a series of actions where the first action sets a TempData value, the next few actions read and then set that TempData value, and the final action reads the TempData value. Pseudo code below...

[HttpPost]
public ActionResult Step1()
{
  TempData["bar"] = foo;
  return RedirectToAction("Step2");
}

public ActionResult Step2()
{
  var foo = TempData["bar"];
  TempData["bar"] = foo;
  return View();
}

[HttpPost]
public ActionResult Step2()
{
  var foo = TempData["bar"];
  TempData["bar"] = foo;
  return RedirectToAction("Step3");
}

public ActionResult Step3()
{
  var foo = TempData["bar"];
  TempData["bar"] = foo;
  return View();
}

[HttpPost]
public ActionResult Step3()
{
  var foo = TempData["bar"];
  return RedirectToAction("AnotherAction", "AnotherController");
}

我的信念是,读书的值之后,将不再提供TempData的。但我开始通过code和加强,而键/值将被添加到TempData的对转让的,它会的从不的消失,当我从TempData的(甚至当我在抵达拉值不同的控制器)。

My belief was that after reading a value, it would no longer be available in TempData. But I started stepping through the code and while the key/value would be added to TempData on assignment, it would never go away when I pulled the value from TempData (even when I arrived in a different controller).

我能够得到它走的唯一方法是手工打的动作,从的TempData

The only way I am able to get it to go away is to manually hit an action that reads from TempData.

任何人都可以提供任何指针,以帮助我更好地了解与的TempData 持久性ASP.NET MVC 2怎么回事?

Can anyone provide any pointers to help me better understand what is going on with TempData persistence in ASP.NET MVC 2?

推荐答案

我要抛出此那里......

I'm going to throw this out there...

RedirectToAction有RedirectToRouteResult的返回类型。这是由几个在上面的伪code中的动作方法调用。

RedirectToAction has a return type of RedirectToRouteResult. This is called by several of the action methods in the above pseudo code.

根据这个可能过时的博客条目 ...

4.RedirectResult和RedirectToRouteResult总是调用
  TempData.Keep()

4.RedirectResult and RedirectToRouteResult always calls TempData.Keep()

调用从动作中保持()
  方法保证没有任何物品的那
  在TempData的在结束时被移除
  当前请求,即使它们
  读。第二过载可以用
  保留在TempData的具体项目。

Calling Keep() from within an action method ensures that none of the items in TempData are removed at the end of the current request, even if they were read. The second overload can be used to retain specific items in TempData.

因此​​,它看起来像我TempData的值被自动标记。我通过观察这些数值在中TempData的_initialKeys出现验证了这一点。

So it looks like my TempData values are being automatically flagged. I verified this by seeing these values show up under _initialKeys within TempData.

这篇关于TempData的坚持阅读ASP.NET MVC 2后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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