TempData 在 ASP.NET MVC 2 中读取后保留

发布于 2024-09-28 08:46:04 字数 1351 浏览 2 评论 0原文

在 ASP.NET MVC 2 中,TempData 值将持续存在,直到会话结束或被读取为止。用微软的话...

TempData 的值一直持续到 它被读取或直到会话时间 出去。以这种方式持久化 TempData 启用重定向等场景, 因为 TempData 中的值是 超出单一请求即可使用。

我以为我理解这一点,但我刚刚在我的应用程序中遇到了异常行为,其中 TempData 值可用,但它不应该可用。一般来说,我有一个包含一系列操作的控制器,其中第一个操作设置 TempData 值,接下来的几个操作读取并设置该 TempData 值,最后一个操作操作读取 TempData 值。下面的伪代码...

[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 中将不再可用。但我开始单步执行代码,虽然键/值会在分配时添加到 TempData,但当我从 TempData 中提取值时,它永远不会消失(即使我到达不同的控制器) 。

我能够让它消失的唯一方法是手动执行从 TempData 读取的操作。

谁能提供任何指示来帮助我更好地理解 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...

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.

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");
}

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).

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

姐不稀罕 2024-10-05 08:46:04

我要把这个扔出去......

RedirectToAction 的返回类型为 RedirectToRouteResult。这是由上面伪代码中的几个操作方法调用的。

根据此可能已过时的博客条目.. 。

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

从操作中调用 Keep()
方法确保没有任何项目
TempData 中的内容在末尾被删除
当前的请求,即使它们是
读。可以使用第二个重载
在 TempData 中保留特定项目。

所以看起来我的 TempData 值已被自动标记。我通过查看这些值显示在 TempData 中的 _initialKeys 下来验证这一点。

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

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

According to this possibly outdated blog entry...

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

and

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.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文