“按请求”共享; ASP.NET MVC 控制器的存储
在标准 ASP.NET Webforms 项目中,如果页面上有多个需要相同数据的用户控件,我倾向于让第一个用户控件将数据放入 HttpContext.Current.Items 集合中,以便可用于所有其他人。
对于在同一页面上多次使用的 MVC 控制器上的 ViewResult,最接近的等效项是什么?
谢谢
编辑:
基本上,这里的 ViewReults 是在页面上多次出现的内容区域,因此我希望查询数据库一次以获取页面所有内容的集合(当第一个内容区域被加载)而不是对每个内容区域进行单独的查询。认为这会加快速度。
更多信息:
为了明确起见,内容控制器操作在由附加“主”控制器提供服务的页面内重复用作“RenderAction”。因此,我认为我不能使用 ViewData (或控制器类本身的任何其他属性),因为它不在同一控制器的不同实例化之间共享。
我想在使用诸如 这个。
In a standard ASP.NET webforms project, if I have a several user controls on a page that require the same data I'd tend to have the first user control put the data in the HttpContext.Current.Items collection so it was available for all the others.
What's the nearest equivalent to this for a ViewResult on an MVC controller that's used multiple times on the same page?
Thanks
Edit:
Basically the ViewReults here are content areas which appear more than once on a page, so I was looking to query the database once to get a collection of all the content for the page (when the first content area is laoded) rather than a separate query for each content area. Thought it would speed things up.
Further info:
Just to make it clear, the content controller action is used repeatedly as a 'RenderAction' from within pages that are served by an additional 'main' controller. Consequently I don't think I can use ViewData (or any other property on the controller class itself) as it's not shared between different instantiations of the same controller.
I think I'm going to have to fall back on the HttpContext after all using a method such as this one.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么不直接将此内容放入 ViewModel 中并让所有控件都使用它?这是视图模型的相当标准的用法。
Why don't you just put this content in your ViewModel and have all the controls use that? This is a pretty standard usage of a View's model.
“同一页面上多次”意味着相同的请求,这意味着您仍然可以使用 HttpContext.Current.Items 集合。
"Multiple times on the same page" means the same request meaning you can still use the HttpContext.Current.Items collection.