ASP.NET MVC局部视图慢? [英] ASP.NET MVC partial views slow?

查看:151
本文介绍了ASP.NET MVC局部视图慢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是碰巧来检查,我们正在建立一个ASP.NET MVC应用程序的性能。我要插入一个局部视图成一个圈,只是出于好奇,我查过了多长时间来渲染页面。其结果是不好的。

我需要做更多的确凿调查,但万一出现了有人用类似问题或更深入的了解,这里是我到目前为止所。首先,我应该说,所有的结果和测量多个页面加载后进行,我已设置<在我的web.config ;编译调试=假过夜。


  • 看来,一个单一的渲染部分即被5ms左右打(至少在我的环境)。当我内联局部视图的实际内容,我得到几乎0毫秒。

  • 当我包括约70元的环空局部视图,总量呈现由〜60ms的时间增加。因此,有一些缓存presumably,但它并不理想。

  • 我调试ASP.NET MVC,并且发现了部分意见被缓存,但它只缓存路径的ASCX的。那么实际的意见是使用实例每次<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.compilation.buildmanager.createinstancefromvirtualpath%28v=VS.90%29.aspx\">BuildManager.CreateInstanceFromVirtualPath方法。

  • 而现在的有趣的一点:当包括使用的WebForms语法(&LT;我:UserContol =服务器/&GT; )相同的局部视图,额外的60ms的走开。

因此​​,基于观察上面,似乎罪魁祸首是BuildManager.CreateInstanceFromVirtualPath方法。也许,这是不是意味着被称为多次。 Web表单presumably不使用它;或者用它在某种程度上只是一次为每个ASCX?


解决方案

我刚刚换了MVC2视图使用的局部视图在一个循环中的单一视图,即:

 &LT;表&gt;
的foreach(在项目风险价值)
{
  &所述;%:Html.Partial(SomePartialView,一)%GT;
}
&LT; /表&gt;

其中SomePartialView包含code键呈现单行表中的,例如

<$p$p><$c$c><tr><td>Model.Name</td><td>Model.description</td></tr>

 的foreach(在项目风险价值)
{
  &所述; TR&GT;&下; TD&GT; a.Name&下; / TD&GT;&下; TD&GT; a.description&下; / TD&GT;&下; / TR&GT;
}

渲染900行的页面渲染时间视图去从5+分钟页面加载下降到不足30秒,pretty确凿的证据是有显著调用的开销局部视图时。我敢肯定,这是微不足道的,当你有一个呼叫,然而,在一个循环这一切意味着,所以我会建议避免在一个循环中,如果可能的局部视图。

I just happen to check the performance of an ASP.NET MVC application we are building. I was going to insert a partial view into a loop, and just out of curiosity I checked how long it took to render the page. The result was not good.

I need to do more conclusive investigation, but just in case there was somebody with similar issues or more insight, here is what I have so far. First, I should say that all results and measurements were done after multiple page loads and that I have set <compilation debug="false"> in my web.config.

  • It seems that a single render partial incurs about 5ms hit (at least in my environment). When I inline the actual content of the partial view, I get practically 0ms.
  • When I include an empty partial view to a loop of about 70 elements, the total render time increases by ~ 60ms. So there is some caching presumably, but it's not ideal.
  • I debugged ASP.NET MVC, and found out that partial views are cached, but it only caches the paths to the ascx's. The actual views are then instantiated every time using the BuildManager.CreateInstanceFromVirtualPath method.
  • And now the interesting bit: When include the same partial view using the WebForms syntax (<my:UserContol runat="server" />), the extra 60ms go away.

So based on the observations above, it seems the culprit is the BuildManager.CreateInstanceFromVirtualPath method. Maybe, it was not meant to be called multiple times. Webforms presumably don't use it; or use it somehow only once for each ascx?

解决方案

I've just changed a MVC2 view from using a partial view in a loop to a single view, i.e. :

<table>
foreach(var a in items)
{
  <%: Html.Partial("SomePartialView",a) %>
}
</table>

Where SomePartialView contains the code to render a single row in a table, e.g. :

<tr><td>Model.Name</td><td>Model.description</td></tr>

to :

foreach(var a in items)
{
  <tr><td>a.Name</td><td>a.description</td></tr>
}

for a view rendering 900 rows the page render time went down from 5+ minutes page load to less than 30 secs, pretty conclusive proof that there is a significant overhead when calling partial views. I'm sure this is negligible when you have a single call, however in a loop it all adds up so I'd recommended avoiding partial views in a loop if possible.

这篇关于ASP.NET MVC局部视图慢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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