gpt4 book ai didi

c# - MVC C#TempData

转载 作者:行者123 更新时间:2023-12-03 13:22:14 30 4
gpt4 key购买 nike

有人可以解释一下MVC中TempData的目的。
我了解它的行为类似于ViewBag,但除此之外,它还能做什么。

最佳答案

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

关于c# - MVC C#TempData,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10487008/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com