ASP.NET MVC - 对于内部的RenderPartial或外循环的RenderPartial [英] ASP.NET MVC - For loop inside RenderPartial or Outside RenderPartial

查看:129
本文介绍了ASP.NET MVC - 对于内部的RenderPartial或外循环的RenderPartial的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

哪一个具有最佳性能。我有一个包含的文章列表列表数组。当我列出的文章我有一个显示只是一篇文章和父页面遍历所有文章的循环中的RenderPartial。该Renderpatial是parentpage循环中。什么是去这个问题的最好方法是什么?

Which one has the best performance. I have a list array that contains a list of articles. When I list the articles I have a RenderPartial that displays just one article and the parent page goes through a loop of all the articles. The Renderpatial is inside the parentpage loop. What is the best way to go about this?

推荐答案

环路局部视图里面,如果你能。每次调用的RenderPartial的时候,下面的事情发生(MVC的源文件提供):

Loop inside the partial view if you can. Each time you call RenderPartial, the following things happen (courtesy of the MVC source files):


  1. 的RenderPartial 要求 RenderPartialInternal

RenderPartialInternal 创建一个新的的ViewDataDictionary 和新的 ViewContext

RenderPartialInternal creates a new ViewDataDictionary and a new ViewContext

RenderPartialInternal 要求 FindPartialView 来定位和创建一个视图

RenderPartialInternal calls FindPartialView to locate and instantiate a view

FindPartialView 搜索所有注册的视图引擎(通常只有一个)的视图中,使用控制器上下文和视图名称作为键。在所有的路径,视图中的每个视图引擎的搜索支持,例如查看/控制器/ view.aspx,视图/控制器/ view.ascx,查看/共享/ view.aspx等视图可以从内存缓存返回到加速这一步

FindPartialView searches all registered view engines (normally just one) for the view, using the controller context and view name as keys. Each view engine searches for the view in all the paths it supports, e.g. Views/controller/view.aspx, Views/controllers/view.ascx, Views/Shared/view.aspx, etc. Views can be returned from a memory cache to accelerate this step

视图的渲染方法被调用。我在13级上下堆叠失去了标准的 WebFormView 渲染法的内部运作的轨道。 渲染构建了大量的上下文对象的内部景观的需求,将检查的权限运行来看,勾搭事件的任何服务器控件,重新检查Request对象来决定它需要做什么更多,等等。实际渲染视图后,开卷它创造的环境。

The view's Render method is called. I lost track of the inner workings of the Render method of the standard WebFormView at 13 levels down the stack. Render constructs a large numbers of context objects the inner view needs, checks for permissions to run the view, hooks up events for any server controls, re-inspects the Request object to decide what more it needs to do, and so on. After actually rendering the view, it unwinds the context it has created.

总体而言,这些都不是太糟糕了。这一切发生机器的CPU和RAM,它比可以为发生在控制器典型数据库存取说更内侧。该过程需要去到磁盘仅在第一次的视图被加载(即可能是缓慢的,然而,文件必须被搜索和视图已被编译)。 ASP.NET MVC不得不优化视图渲染过程中保持较高的性能水平。

Overall, none of these is too bad. It all happens inside the machine's CPU and RAM, which is more than can be said for typical database access that happens in the controller. The process needs to go out to disk only the first time the view is loaded (that can be slow, however; files have to be searched and the view has to be compiled). ASP.NET MVC had to optimize the view rendering process to maintain a high level of performance.

不过,这是相当多的,如果你能避免一个请求多次运行它,它有利于提高操作方法的响应时间。

Still, this is quite a bit, and if you can avoid running it multiple times in one request, it will help improve the action method's response times.

这篇关于ASP.NET MVC - 对于内部的RenderPartial或外循环的RenderPartial的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆