TempData 在 ASP.NET MVC 2 中读取后保留
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我要把这个扔出去......
RedirectToAction 的返回类型为 RedirectToRouteResult。这是由上面伪代码中的几个操作方法调用的。
根据此可能已过时的博客条目.. 。
和
所以看起来我的 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...
and
So it looks like my TempData values are being automatically flagged. I verified this by seeing these values show up under _initialKeys within TempData.