为什么TempData的背靠会议 [英] Why is TempData backed by Session

查看:115
本文介绍了为什么TempData的背靠会议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已阅读,TempData的是通过会议Asp.Net MVC的支持,但仅适用于当前请求的持续时间。这使得它适合Redirect2Action等。
鉴于它仅适用于当前请求,这是否意味着你不会得到与负载平衡器和会议WebFarms问题。基本上,因为你只能在每次请求的Web服务器。

难道是由Request.Items收集来支持它的选项呢?


解决方案

TempData的仅适用于当前和<一个href=\"http://stackoverflow.com/questions/473520/asp-net-tempdata-persists-between-requests\">next申请这就是为什么它被存储在会话状态。该请求将其存储是行不通的。

因此​​,这将打破保存在会话状态项目的任何限制(例如负载平衡)将打破TempData的为好。

请参见 TempDataDictionary <文档/ p>


  

可以使用TempDataDictionary对象以相同的方式传递数据
  您使用的ViewDataDictionary对象。但是,在一个数据
  TempDataDictionary对象仍然存在只能从一个请求到下一​​个,
  除非您使用保持标记为保留一个或多个密钥
  方法。如果一个键被标记为保留,关键是保留的
  下一个请求。


  
  

对于TempDataDictionary对象的典型应用是通过从数据
  当它重定向到另一个操作方法操作方法。例如,
  操作方法可能会存储有关的错误信息
  控制器的TempData属性(返回TempDataDictionary
  对象),它调用RedirectToAction方法之前。下一个动作
  那么方法能处理错误并呈现一个显示的视图
  错误消息。


I have read that TempData is backed by session in Asp.Net MVC, but only valid for the duration of the current request. This makes it appropriate for Redirect2Action etc. Given that it's only valid for the current request, does that mean that you won't get into issues with load balancers and session on WebFarms. Basically because you're only on one webserver per request.

Would it be an option to back it by the Request.Items collection instead?

解决方案

TempData is only valid for the current and next request which is why it is stored in the session state. Storing it with the request would not work.

So any limitations that would break saving items in session state (e.g. load balances) will break TempData as well.

See the documentation for 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.

这篇关于为什么TempData的背靠会议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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