告诉操作/控制器保留 TempData
据我了解,TempData 设计为仅在单页请求之间工作。但我认为有一种情况破坏了预期的功能。
我优先使用控制器而不是处理程序来传递图像。我现在不知道这是否是最佳实践,但它对我来说非常有效。但问题是,对图像操作之一的每次调用显然都会耗尽 TempData 信用。
MVC 中有没有办法说“此控制器/操作超出了正常页面请求的范围”,因此要么保留 TempData,要么将其自身完全从 TempData 生命周期中删除?
富有的
I understand that TempData is designed to work only between a single page request. But I think have a situation I think breaks the intended functionality.
I use Controllers in preference to Handlers for delivering images. I don't now if that is best practice, but it works very well for me. The problem though, is that each call to one of the image Actions obviously uses up a TempData credit.
Is there a way in MVC to say "This Controller/Action is out of the scope of normal page requests" and therefore either persist the TempData or remove itself from the TempData life cycle completely?
Rich
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我的解决方案是创建一个在页面请求之间保留 TempData 的属性。我对此的最初反应是“恶心”,但我想有效地从 TempData 生命周期中排除任何用该属性修饰的控制器。
任何有关更好解决方案的反馈将不胜感激。
富有的
My solution has been to create an Attribute that persists the TempData across page requests. My initial reaction to this is "yuck", but I want to effectively exclude any controllers decorated with the Attribute from the TempData lifecycle.
Any feedback on a better solution would be appreciated.
Rich
我不确定这个问题的确切答案,因为我自己对 MVC 还很陌生。然而,我听说有人在处理 AJAX 请求时遇到麻烦,并且在他们想要之前就终止了 TempData。
我想您可以实现自己的基于会话的系统来存储这些信息?
不过,我相信其他人很快就会为您提供更完整的答案!
I'm not sure of the exact answer to this as I'm pretty new to MVC myself. However, I have heard of people having trouble with AJAX requests killing off the TempData before they wanted it to as well.
I guess you could implement your own Session based system to store this information?
I'm sure someone else will have a more complete answer for you soon, though!
我不完全理解你的做法和原因,但我宁愿选择处理程序。或者,如果您坚持使用控制器操作,我可以说这对我来说效果很好,没有任何动态传递图像的问题,并且我在控制器操作中使用了 FileContentResult。
像这样:
您从数据存储区或其他地方获取字节..
希望它有帮助
I do not completely understand your way of doing it and why but I would rather go with handlers. Or if you stick to controller actions, I can say that this worked for me fine without any issues for delivering images on-the-fly and I used FileContentResult in my controller actions.
Like this:
You get the bytes out of datastore or somewhere..
hope it helps