Soe*_*Moe 5 performance asp.net-mvc-2
在我最近使用Asp.net Mvc 2的项目中,我们发现DisplayFor存在性能问题.我不确定这是真的问题还是我错过了什么?
我希望Asp.net Mvc Guru可以向我解释一下.:)
模型.
public class Customer
{
public int CustomerId { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public string EmailAddress { get; set; }
public static IEnumerable<Customer> GetCustomers()
{
for (int i = 0; i < 1000; i++)
{
var cust = new Customer()
{
CustomerId = i + 1,
Name = "Name - " + (i + 1),
Address = "Somewhere in the Earth...",
EmailAddress = "customerABC"
};
yield return cust;
}
}
}
Run Code Online (Sandbox Code Playgroud)
调节器
public ActionResult V1()
{
return View(Customer.GetCustomers());
}
public ActionResult V2()
{
return View(Customer.GetCustomers());
}
Run Code Online (Sandbox Code Playgroud)
V1(性能问题)
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<Customer>>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
V1
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>V1</h2>
<table>
<%foreach (var cust in this.Model)
{%>
<%= Html.DisplayFor(m => cust) %>
<%} %>
</table>
</asp:Content>
Run Code Online (Sandbox Code Playgroud)
而模板是
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Customer>" %>
<tr>
<td><%= this.Model.CustomerId %></td>
<td><%= this.Model.Name %></td>
<td><%= this.Model.Address %></td>
<td><%= this.Model.EmailAddress %></td>
</tr>
Run Code Online (Sandbox Code Playgroud)
V2(无性能问题)
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<Customer>>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
V2
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>V2</h2>
<table>
<%foreach (var cust in this.Model)
{%>
<tr>
<td><%= cust.CustomerId%></td>
<td><%= cust.Name%></td>
<td><%= cust.Address%></td>
<td><%= cust.EmailAddress%></td>
</tr>
<%} %>
</table>
</asp:Content>
Run Code Online (Sandbox Code Playgroud)
我可以很容易地看到V1和V2之间的性能差异.
编辑:当我部署到我的本地IIS 7(与发布版本),它(V1)变得非常快.问题解决了,但我仍然想知道原因.:)
谢谢,
Soe Moe
Lev*_*evi 12
仅在发布模式下启用缓存.如果在调试模式下运行应用程序,则可能会因磁盘访问而导致性能下降.
另见:http: //codeclimber.net.nz/archive/2009/04/22/how-to-improve-htmlhelper.renderpartial-performances-donrsquot-run-in-debug-mode.aspx