MVC C#的TempData [英] MVC C# TempData

查看:705
本文介绍了MVC C#的TempData的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人能请解释一下TempData的目的MVC中。
我理解它像ViewBag但它有什么作用不止于此。

 的TempData


解决方案

  

TempData的,就是要一个非常短暂的实例,你应该
  仅在当前,只有后续请求使用它!以来
  TempData的这种方式工作,你需要肯定知道下一个
  要求将和重定向到另一个观点是唯一的一次
  可以保证这一点。因此,唯一的方案,其中使用的TempData
  将可靠地工作是当你重定向。这是因为一个
  重定向杀死当前请求(并发送HTTP状态code 302
  对象移到客户端),然后在服务器上创建一个新的请求
  服务重定向的视图。回首previous
  HomeController的code样品意味着TempData的对象可能会产生
  结果与预期不同,因为下一个请求起源
  不能保证。例如,下一个请求可以从发起
  一个完全不同的机器和浏览器实例。


<一个href=\"http://rachelappel.com/when-to-use-viewbag-viewdata-or-tempdata-in-asp.net-mvc-3-applications\">http://rachelappel.com/when-to-use-viewbag-viewdata-or-tempdata-in-asp.net-mvc-3-applications

Can somebody please explain the purpose of TempData in MVC. I understand it behaves like ViewBag but what does it do beyond that.

    TempData

解决方案

TempData is meant to be a very short-lived instance, and you should only use it during the current and the subsequent requests only! Since TempData works this way, you need to know for sure what the next request will be, and redirecting to another view is the only time you can guarantee this. Therefore, the only scenario where using TempData will reliably work is when you are redirecting. This is because a redirect kills the current request (and sends HTTP status code 302 Object Moved to the client), then creates a new request on the server to serve the redirected view. Looking back at the previous HomeController code sample means that the TempData object could yield results differently than expected because the next request origin can't be guaranteed. For example, the next request can originate from a completely different machine and browser instance.

http://rachelappel.com/when-to-use-viewbag-viewdata-or-tempdata-in-asp.net-mvc-3-applications

这篇关于MVC C#的TempData的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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