我們有一個MVC 4 Web應用程序。我使用miniprofiler來檢查它需要多長時間,並且它顯示具有三個部分視圖的視圖渲染需要很長時間。我嘗試將部分視圖中的信息直接移動到頁面上,這對時代根本沒有影響。我應該怎麼做才能減少渲染時間?任何提示我應該從哪裏開始尋找?正如你所看到的,渲染需要48秒。渲染視圖需要很長的時間
**| duration | from start(ms)**
|http://localhost:61380/CaseContacts | 653.7 | +0.0
|Controller: CaseController.Contacts | 11.9 | +641.1
| Contacts view - not an ajax request | 0.3 | +651.7
| populating view model | 0.0 | +652.1
| getting list of contacts | 374.4 | +652.1
| Find: Contacts | 499.8 | +1033.6
| Render: Contacts | 48400.8 | +1535.2
**| client event |duration(ms)| from start(ms)**
|Request start | | +2.0
|Response | 1.0 | +52245.0
|Dom loading | | +52247.0
|scripts | 390.0 | +52315.0
|Dom Content Loaded Event| 29.0 | +52707.0
|Dom Interactive | | + 52707.0
|Load Event | | + 52760.0
|Dom Complete | | + 52760.0
更新:
public IQueryable<T> FindQueryable(Expression<Func<T, bool>> predicate, Func<T, Object> orderBy)
{
return this.db.GetAll<T>().Where(predicate).OrderBy(orderBy).AsQueryable<T>();
}
public class ContactRepository : Repository<USRContact>, IContactRepository<USRContact>
{
public IList<ContactNameViewModel> GetAll(int fieldOfficeID, string searchText = "")
{
if (searchText.Length > 0)
return Mapper.Map<IList<USRContact>, IList<ContactNameViewModel>>(this.FindQueryable(x => (x.FieldOfficeID == fieldOfficeID) &&
(x.FormalName.Contains(searchText) || x.PreferredName.Contains(searchText) || x.Alias.Contains(searchText) || x.Pseudonym.Contains(searchText)), x => (x.STMGender.Gender)).ToList());
else
return Mapper.Map<IList<USRContact>, IList<ContactNameViewModel>>(this.FindQueryable(x => (x.FieldOfficeID == fieldOfficeID)).ToList());
}
然後,我的控制器被加載的結果到一個視圖模型,然後:
@Html.Partial("~/Views/Shared/Contacts/ContactNameList.cshtml", Model.Contacts)
局部視圖包含以下代碼:
@model IList<Casenator.Web.Models.Contact.ContactNameViewModel>
<ul id="NameList" style="margin-left:0">
@foreach (var item in Model)
{
@*<li>@Html.RouteLink(@item.FullName, "Contacts", new { id = item.ContactID })</li>*@
<li>@Ajax.RouteLink(item.FullName, "Contacts", new { id = item.ContactID }, new AjaxOptions { UpdateTargetId = "BasicInfo", OnSuccess="InitializeComboBoxes", OnBegin="LoadContact(" + item.ContactID + ")" }) </li>
}
</ul>
任何幫助將不勝感激! 謝謝, Safris
只是一個大膽的想法,如果ü做tolist到您的查詢結果是否提高任何性能 – Tassadaque 2013-03-04 21:33:47
我的查詢結果實際上是回來作爲一個列表發生了什麼。 – safriss 2013-03-04 22:44:46