c# - 为什么 TempData 由 Session 支持

标签 c# asp.net asp.net-mvc

我读到 TempData 由 Asp.Net MVC 中的 session 支持,但仅在当前请求期间有效。这使其适用于 Redirect2Action 等。 鉴于它仅对当前请求有效,这是否意味着您不会遇到 WebFarm 上的负载平衡器和 session 问题。基本上是因为每个请求只在一个网络服务器上。

是否可以选择由 Request.Items 集合来支持它?

最佳答案

TempData 仅对当前和 next request 有效这就是它存储在 session 状态中的原因。将其与请求一起存储是行不通的。

因此,任何会破坏在 session 状态中保存项目的限制(例如负载平衡)也会破坏 TempData。

请参阅 TempDataDictionary 的文档

You can use a TempDataDictionary object to pass data in the same way that you use a ViewDataDictionary object. However, the data in a TempDataDictionary object persists only from one request to the next, unless you mark one or more keys for retention by using the Keep method. If a key is marked for retention, the key is retained for the next request.

A typical use for a TempDataDictionary object is to pass data from an action method when it redirects to another action method. For example, an action method might store information about an error in the controller's TempData property (which returns a TempDataDictionary object) before it calls the RedirectToAction method. The next action method can then handle the error and render a view that displays an error message.

关于c# - 为什么 TempData 由 Session 支持,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9987341/

相关文章:

c# - 尝试解析 double 值

c# - 如何使用同一数据表的第一行更改数据表的列名值

asp.net-mvc - kendo upload allowedmultiple autoupload 多次调用API

c# - asp.net如何同时执行多条sql命令

asp.net-mvc - 从 Azure 网站取消发布 MVC 4 项目

asp.net-mvc - “个人”是一种类型,在给定上下文中无效

c# - 删除给定索引号以外的所有元素

c# - WriteLineAsync 与取消

c# - Java 等效于 C# XML 签名方法

javascript - 使用 ASP :DropDownList is proving difficult 中的值填充弹出窗口