我如何加速我的asp.net mvc应用程序?

时间:2014-06-18 14:50:42

标签: asp.net-mvc performance pageload

我目前正在开展一个项目,我必须从数据库中提取数据并将其显示在我视图中的表格中。我已经完成了所有这些工作,但加载页面大约需要10-15秒。如果可能的话,我想减少这段时间。

我认为问题在于从数据库中获取信息。我从数据库中提取了很多项目,我相信可能有更好的方法。

控制器:

 public class HomeController : Controller
{
    private RestoreDBEntities db = new RestoreDBEntities();

    public ActionResult Index()
    {
        W6ViewModel viewModel = new W6ViewModel();
        viewModel.engineers = db.W6ENGINEERS.OrderBy(w => w.Name).Include(w => w.W6CALENDARS).ToList();
        viewModel.tasks = db.W6TASKS.ToList();

        return View(viewModel);
    }

    public ActionResult Details(int? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        W6ENGINEERS w6ENGINEERS = db.W6ENGINEERS.Find(id);
        if (w6ENGINEERS == null)
        {
            return HttpNotFound();
        }
        return View(w6ENGINEERS);
    }

    [HttpPost]
    public ActionResult Filter(FormCollection collection)
    {
        string city = collection["city"];

        W6ViewModel viewModel = new W6ViewModel();
        viewModel.engineers = db.W6ENGINEERS.Where(w => w.City == city).Include(w => w.W6CALENDARS).ToList();
        viewModel.tasks = db.W6TASKS.Where(w => w.City == city).Include(w => w.W6CALENDARS).ToList();

        return View("Index", viewModel);
    }

查看:

@model WebApplication1.ViewModel.W6ViewModel

@{
    ViewBag.Title = "Dispatcher";
    Layout = "~/Views/Shared/_Layout.cshtml";
}


<section id="fields">
    <h3 id="field_filter"><strong>Filters</strong></h3>
    <h3 id="field_engineer"><strong>Engineers</strong></h3>
    <h3 id="field_task"><strong>Tasks</strong></h3>
</section>

<section id="filters">
    @using (Html.BeginForm("Filter", "Home", FormMethod.Post))
    { 
        <input name="city" id="city" type="text" maxlength="15" title="City" value    ="City" style="color:#888;" 
         onfocus ="inputFocus(this)" onblur="inputBlur(this)" />
         <input id="submit" type="submit" value="Submit" />`enter code here`
    }
 </section>

 <section id="engineers">
     <table class="table-condensed table-striped">
        <tr>
            <th>
                Name
            </th>
            <th>
                Phone number
            </th>
        <th>
            City
        </th>
        <th>
            Region
        </th>
        <th>
            Availability Factor
        </th>
    </tr>

        @foreach (var item in Model.engineers)
        {
            <tr>
                <td>
                    @Html.ActionLink(item.Name, "Details", new { id = item.W6Key })
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.MobilePhone)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.City)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Region)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.AvailabilityFactor)
                </td>
            </tr>
        }
    </table>
</section>

<section id="work">
    <table class="table-condensed table-striped">
        <tr>
            <th>
                Job ID
            </th>
            <th>
                Skills 
            </th>
            <th>
                Address
            </th>
        </tr>

    @foreach(var item in Model.tasks)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.City)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.IsScheduled)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.IsPartsNotUsed)
            </td>
        </tr>
    }
</table>

Webconfig:

   <?xml version="1.0" encoding="utf-8"?>

<configuration>
<configSections>
<section name="entityFramework"   type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework,  Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"  requirePermission="false" />
  </configSections>
  <connectionStrings>
    <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-WebApplication1-20140611092404.mdf;Initial Catalog=aspnet-WebApplication1-20140611092404;Integrated Security=True" providerName="System.Data.SqlClient" />
    <add name="RestoreDBEntities" connectionString="metadata=res://*/Models.Model1.csdl|res://*/Models.Model1.ssdl|res://*/Models.Model1.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=T520-R9K0H1K\SQLEXPRESS;initial catalog=RestoreDB;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
  <appSettings>
    <add key="webpages:Version" value="3.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  </appSettings>
  <system.web>
     <authentication mode="None" />
    <compilation debug="true" targetFramework="4.5.1" />
    <httpRuntime targetFramework="4.5.1" />
  </system.web>

  <system.webServer>
    <urlCompression doDynamicCompression="true" doStaticCompression="true" dynamicCompressionBeforeCache="true"/>
  </system.webServer>

  <system.webServer>
    <modules>
      <remove name="FormsAuthenticationModule" />
    </modules>
  </system.webServer>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35"    />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-5.1.0.0" newVersion="5.1.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
</configuration>

提前多多谢谢!

编辑:

所以在安装DotTrace并使用它来诊断我的项目后,似乎是我对数据库的调用导致了很长的加载时间(我的意见,虽然我没有使用DotTrace的经验)

我添加了截图,如果有人可以确认/提出一个很棒的解决方案。

DotTrace Snapshot upon running the project

1 个答案:

答案 0 :(得分:0)

您可以做的一件事就是缓存输出。这将节省后续请求的时间。您可以在要缓存的任何操作方法上使用输出缓存属性。

[OutputCache(Duration = 3600, Location = System.Web.UI.OutputCacheLocation.ServerAndClient)]

持续时间是您希望以秒为单位缓存输出的时间长度,并且该位置是您要缓存它的位置。

您还可以使用SqlCacheDependency缓存请求,直到数据更新为止。将输出缓存属性与sql依赖关系一起使用可能更适合您的问题。 以下是有关如何在msdn - http://msdn.microsoft.com/en-us/library/e3w8402y(v=vs.140).aspx上执行此操作的演练。

您还可以采取其他措施来加速您的mvc应用。我写了一篇关于你可以做的一些事情的简短帖子。在这里查看它们 - http://www.paulsodimu.co.uk/Post/How-to-speed-up-your-MVC-web-application