ASP.NET MVC - TempData - 好的或坏的实践
我正在使用 Scott Gu 的 Preview 5 博客文章中详细介绍的 AcceptVerbs 方法来处理 ASP.NET MVC 中的表单条目:
- 用户通过 GET 获取空表单
- 用户通过 POST 将填写的表单发布到相同的操作
- 该操作验证数据,采取适当的操作,然后重定向到新视图
,因此我不必使用TempData
。 也就是说,我现在必须为此过程添加一个“确认”步骤,并且似乎需要使用 TempData。
出于某种原因,我讨厌使用 TempData——因为它是需要围绕它进行设计的东西。
这是一个合理的担忧,还是我编造的?
I'm using the AcceptVerbs
method detailed in Scott Gu's Preview 5 blog post for dealing with form entries in 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
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
.
For some reason, I have an aversion to using TempData
-- that it is something to be designed around.
Is this at all a valid concern, or am I making it up?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
没有必要厌恶 TempData...但如果使用不当,它肯定会表明设计不佳。 如果您使用 RESTful URL,则 TempData 是将消息从 POST 操作传输到 GET 操作的最佳实践。 考虑一下:
您在 URL Products/New 处有一个表单。 表单发布到 Products/Create,验证表单并创建产品,成功时控制器重定向到 URL Products/1,出错时将重定向回 products/New 以显示错误消息。
Products/1 只是产品的标准 GET 操作,但我们希望显示一条消息,指示插入成功。 TempData 非常适合此目的。 将消息添加到 Post Controller 中的 TempData 中,并在视图中添加一些 if 逻辑,然后就完成了。
失败时,我将在 formCollection 中输入的值和错误消息集合添加到 Post Action 中的 TempData,并重定向到初始 Action Prodcuts/New。
我已向视图添加逻辑,以使用之前输入的值以及任何错误消息来填充表单输入。 对我来说看起来又漂亮又干净!
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:
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.
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.
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!
我认为您在使用 TempData 之前最好先犹豫一下。 TempData 存储在会话中,如果出现以下情况,这可能会对您产生影响:
如果您的站点需要具有高可用性,那么在应用会话状态方面还有其他考虑因素,但这些都是可以解决的问题。
I think you do well to hesitate before using TempData. TempData is stored in the session and this may have implications for you if:
If your site needs to have high availability, then there are additional considerations around applying session state but these are all solvable problems.
我认为临时数据是一种通知用户的即发即忘机制。 很高兴让他们提醒他们最近所做的事情,但我也犹豫是否将其作为某些用户流程中的必需步骤。 原因是如果他们刷新页面,我相信它就会消失。 好吧,我想我也犹豫是否使用它,因为它没有很好地定义它的可靠性。
我想知道问题是否在于您在确认步骤之前将操作重定向到另一个页面。 我想知道在他们第一次提交后,您是否可以进行足够的处理来生成确认对话框,然后返回带有确认问题的原始页面。 与验证的方式类似,不同之处在于验证规则检查是否执行了确认步骤(在其他验证通过之前隐藏确认 UI)。
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).
我有一个 GetModel 方法,它首先检查 TempData["model"] 并返回它。 否则,GetModel 从数据库加载适当的数据。
当我有一个操作需要返回需要相同模型数据的不同视图时,它可以节省数据库的额外负载。
I have a GetModel method which first checks for TempData["model"] and returns that. Otherwise GetModel loads the appropriate data from the database.
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.
查看无会话控制器在MVC3中。 事实证明,使用会话会阻止单个用户请求的并行执行,从而导致性能下降。
由于 tempdata 默认使用会话,因此您将无法使用此功能。 您可以改用 cookie 来存储临时数据,但这有点尴尬(至少对我来说)。 不过,仍然比视图状态更干净,所以也许这并不是什么大问题。
Check out sessionless controllers in MVC3. It turned out, that using session prevents parallel execution of a single user's requests and thus leads to degraded performance.
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.
为什么你有这样的厌恶? 这个东西只是完成它的工作并做得很好:)
如果你因为它是非强类型而不喜欢它,你总是可以制作一个包装器来为你提供强类型接口。
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.
这就像使用 ViewData,这意味着它可能不存在安全风险。 但我宁愿使用 ViewData 而不是 TempData。 检查此处进行比较: http://www.squaredroot .com/2007/12/20/mvc-viewdata-vs-tempdata/
根据设计,您始终可以将用户/购物篮或任何您需要的内容存储在数据库的临时数据中,并且只需有一个“IsReady” ” 字段,指示其是否已完成,如果您想记住,人们可以关闭浏览器,则可以稍后扩展它。
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/
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.
所有好的答案,您是否看过这个用于传递消息的内容。
TempData 和 Session 并不是 RESTful 架构的最佳选择,因为大多数会话都存储在内存中。 因此,当您想要使用服务器场时,用户会话将存在于一台服务器上,而他们的下一个请求可能会发送到另一台服务器。
话虽如此,请查看此处使用 TempData 传递消息的情况。
http://jameschambers.com/2014/ 06/day-14-bootstrap-alerts-and-mvc-framework-tempdata/
Mabye 如果仅用于重定向到另一个页面警报,则可以调整为使用查询字符串方法。
All good answers, have you had a look at this for passing messages along.
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.
That being said have a look at this use of TempData for passing messages here.
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.