asp.net-mvc ASP.NET MVC - TempData - 好的或坏的做法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/386681/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-07 23:13:44  来源:igfitidea点击:

ASP.NET MVC - TempData - Good or bad practice

asp.net-mvctempdata

提问by anonymous

I'm using the AcceptVerbsmethod detailed in Scott Gu's Preview 5 blog post for dealing with form entries in ASP.NET MVC:

我正在使用AcceptVerbsScott Gu 的 Preview 5 博客文章中详述的方法来处理 ASP.NET MVC 中的表单条目:

  • User gets an empty form via GET
  • User posts the filled in form via POST to the same Action
  • The Action validates data, takes appropriate action, and redirects to a new view
  • 用户通过 GET 获取一个空表单
  • 用户通过 POST 将填写的表单发布到同一个 Action
  • Action 验证数据,采取适当的行动,并重定向到一个新的视图

So I don't have to use TempData. That said, I now have to add a 'confirm' step to this process, and it seems to require the use of TempData.

所以我不必使用TempData. 也就是说,我现在必须在这个过程中添加一个“确认”步骤,它似乎需要使用TempData.

For some reason, I have an aversion to using TempData-- that it is something to be designed around.

出于某种原因,我不喜欢使用TempData- 它是要围绕它设计的东西。

Is this at all a valid concern, or am I making it up?

这是一个有效的问题,还是我编造的?

采纳答案by Frank Schwieterman

I kind of think of temp data as being a fire-and-forget mechanism for notifying the user. Its great to give them a reminder of something they recently did, but I'd also be hesitant to make it a required step in some user process. The reason being if they refresh the page, I believe it would be gone. Well I guess I'm also hesitant to use it as its not really well defined how reliable it is.

我有点认为临时数据是一种通知用户的即发即弃机制。很高兴提醒他们他们最近所做的事情,但我也不愿将其作为某些用户流程中的必需步骤。原因是如果他们刷新页面,我相信它会消失。好吧,我想我也不愿使用它,因为它并没有很好地定义它的可靠性。

I wonder if the problem is that you're having the action redirect to another page before the confirm step. I wonder if instead after they first submit, you could do enough processing to generate the confirm dialog, then return the original page with the confirm question. Similar to how you might do validation, except the validation rule checks whether the confirmation step was performed (with the confirmation UI hidden until other validation passes).

我想知道问题是否是您在确认步骤之前将操作重定向到另一个页面。我想知道在他们第一次提交之后,您是否可以做足够的处理来生成确认对话框,然后返回带有确认问题的原始页面。与您进行验证的方式类似,除了验证规则检查是否执行了确认步骤(在其他验证通过之前隐藏确认 UI)。

回答by JasonD

No need to have an aversion to TempData... But if not used correctly it could surely be an indication of poor design. If you are using RESTful URL's, TempData is a best practice for transfering messages from your POST Actions to your GET Actions. Consider this:

不需要对 TempData 有反感...但如果使用不当,它肯定会表明设计不佳。如果您使用 RESTful URL,则 TempData 是将消息从 POST 操作传输到 GET 操作的最佳实践。考虑一下:

You have a form at URL Products/New. The form Posts to Products/Create, which validates the form and creates the Product, On Success the Controller redirects to URL Products/1 and on error would redirect back to products/New to display Error Messages.

您在 URL Products/New 有一个表单。表单发布到产品/创建,它验证表单并创建产品,成功时控制器重定向到 URL Products/1,错误时将重定向回产品/新以显示错误消息。

Products/1 is just the standard GET action for the product, but we would like a message to display indicating the insert was a success. TempData is perfect for this. Add the message to TempData in the Post Controller and put some if logic in the view and your done.

Products/1 只是产品的标准 GET 操作,但我们希望显示一条消息,表明插入成功。TempData 是完美的。将消息添加到 Post Controller 中的 TempData 并在视图中放置一些 if 逻辑并完成。

On failure I've been adding the values entered in the formCollection and a collection of error Messages to TempData in the Post Action, and redirecting to the intial Action Prodcuts/New. I've added logic to the view to populate the form inputs with the previously entered values along with any error messages. Seems nice and clean to me!

失败时,我一直在将 formCollection 中输入的值和一组错误消息添加到 Post Action 中的 TempData,并重定向到初始 Action Prodcuts/New。我已经向视图添加了逻辑,以使用先前输入的值以及任何错误消息填充表单输入。对我来说看起来很好很干净!

回答by John Rayner

I think you do well to hesitate before using TempData. TempData is stored in the session and this may have implications for you if:

我认为你最好在使用 TempData 之前犹豫一下。TempData 存储在会话中,如果出现以下情况,这可能会对您产生影响:

  1. You don't use sessions on your site right now
  2. You have a system that needs to scale to high throughput, i.e. you'd prefer to avoid session state altogether
  3. You don't want to use cookies (I don't know how well MVC supports cookieless sessions right now)
  1. 您现在不在您的网站上使用会话
  2. 您有一个需要扩展到高吞吐量的系统,即您希望完全避免会话状态
  3. 您不想使用 cookie(我现在不知道 MVC 对 cookieless 会话的支持程度如何)

If your site needs to have high availability, then there are additional considerations around applying session state but these are all solvable problems.

如果您的站点需要具有高可用性,那么在应用会话状态方面还有其他注意事项,但这些都是可以解决的问题。

回答by aaimnr

Check out sessionless controllersin MVC3. It turned out, that using session prevents parallel execution of a single user's requests and thus leads to degraded performance.

查看MVC3 中的无会话控制器。事实证明,使用 session 会阻止并行执行单个用户的请求,从而导致性能下降。

Since tempdata uses session by default you wouldn't be able to use this feature. You can switch to using cookies for tempdata, but it's a bit awkward (at least for me). Still cleaner than viewstate, though, so maybe it's not such a big dealbreaker.

由于 tempdata 默认使用 session,因此您将无法使用此功能。您可以切换到对临时数据使用 cookie,但这有点尴尬(至少对我而言)。尽管如此,仍然比 viewstate 更干净,所以也许它不是什么大不了的破坏者。

回答by Todd Smith

I have a GetModel method which first checks for TempData["model"] and returns that. Otherwise GetModel loads the appropriate data from the database.

我有一个 GetModel 方法,它首先检查 TempData["model"] 并返回它。否则 GetModel 从数据库加载适当的数据。

It saves an extra load from the database when I have an action that needs to return a different view that requires the same model data.

当我有一个操作需要返回需要相同模型数据的不同视图时,它可以节省数据库的额外负载。

回答by Filip Ekberg

It's like using ViewData, meaning it's probably not a security risk. But i would rather use ViewData than TempData. Check here for a comparason: http://www.squaredroot.com/2007/12/20/mvc-viewdata-vs-tempdata/

这就像使用 ViewData,这意味着它可能不是安全风险。但我宁愿使用 ViewData 而不是 TempData。在此处查看比较:http://www.squaredroot.com/2007/12/20/mvc-viewdata-vs-tempdata/

Depending on the design, you could always store the user / basket or whathever you need in the tempdata in the database and just have a "IsReady" field which indicates if its completed or not, making it extensable for later if you want to take in mind, that people can close their browsers.

根据设计,您可以始终将用户/购物篮或您需要的任何内容存储在数据库的临时数据中,并且只有一个“IsReady”字段,指示其是否已完成,如果您想接收,则可以在以后扩展它请注意,人们可以关闭浏览器。

回答by maxnk

Why do you have such an aversion? This thing is simply make its job and make it well :)

你为什么会有这种厌恶感?这件事只是做好它的工作并做好:)

If you don't like it because of it non-strongly-typed, you can always make a wrapper around which will provide you strongly-typed interface.

如果你不喜欢它因为它是非强类型的,你总是可以制作一个包装器,它会为你提供强类型的接口。

回答by Warren

All good answers, have you had a look at this for passing messages along.

所有好的答案,您是否看过this以传递消息。

TempData and Session arent the best idea for RESTful architectures as most sessions are stored in memory. So when you want to use a server farm, the users session would exist on one server while their next request could be sent to another server.

TempData 和 Session 不是 RESTful 架构的最佳想法,因为大多数会话都存储在内存中。因此,当您想使用服务器群时,用户会话将存在于一台服务器上,而他们的下一个请求可能会发送到另一台服务器。

That being said have a look at this use of TempData for passing messages here.

话虽如此,请在此处查看使用 TempData 传递消息的方法。

http://jameschambers.com/2014/06/day-14-bootstrap-alerts-and-mvc-framework-tempdata/

http://jameschambers.com/2014/06/day-14-bootstrap-alerts-and-mvc-framework-tempdata/

Mabye this could be adapted to use a query string approach if used only for redirect to another page alerts.

如果仅用于重定向到另一个页面警报,这可能适用于使用查询字符串方法。