Asp.Net MVC 5 Razor慢视图渲染

时间:2017-05-18 17:20:45

标签: asp.net-mvc performance razor view rendering

我的Web应用程序性能有问题。

我的应用程序托管在Azure上,服务器B1 Basic,使用代码优先的数据库和EF6。

在一些包含大约300行的页面中,视图渲染大约是35秒,这个问题 不鼓励用户使用app。

当我限制为10行时,视图渲染大约为3秒。

我已经完成了性能视图渲染的几乎技巧。

如果您需要更多信息,请告诉我,谢谢。

IMG - View with bad performance

Global.asax中

protected void Application_Start()
{
    var ptBRCulture = new CultureInfo("pt-BR");

    Thread.CurrentThread.CurrentCulture = ptBRCulture;
    Thread.CurrentThread.CurrentUICulture = ptBRCulture;

    AreaRegistration.RegisterAllAreas();
    ModelBinders.Binders.Add(typeof(decimal), new DecimalModelBinder());
    ModelBinders.Binders.Add(typeof(double), new DoubleModelBinder());
    GlobalConfiguration.Configure(WebApiConfig.Register);
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    RouteConfig.RegisterRoutes(RouteTable.Routes);
    BundleConfig.RegisterBundles(BundleTable.Bundles);

    ViewEngines.Engines.Clear();
    ViewEngines.Engines.Add(new ProfilingViewEngine(new CSHtmlRazorViewEngine()));

    ClientDataTypeModelValidatorProvider.ResourceClassKey = "Messages";
    DefaultModelBinder.ResourceClassKey = "Messages";
}

CSHtmlRazorViewEngine.class

public class CSHtmlRazorViewEngine : RazorViewEngine
{
    public CSHtmlRazorViewEngine()
    {
        AreaViewLocationFormats = new string[] { "~/Areas/{2}/Views/{1}/{0}.cshtml", "~/Areas/{2}/Views/Shared/{0}.cshtml" };
        AreaMasterLocationFormats = new string[] { "~/Areas/{2}/Views/{1}/{0}.cshtml", "~/Areas/{2}/Views/Shared/{0}.cshtml" };
        AreaPartialViewLocationFormats = new string[] { "~/Areas/{2}/Views/{1}/{0}.cshtml", "~/Areas/{2}/Views/Shared/{0}.cshtml" };
        ViewLocationFormats = new string[] { "~/Views/{1}/{0}.cshtml", "~/Views/Shared/{0}.cshtml" };
        MasterLocationFormats = new string[] { "~/Views/{1}/{0}.cshtml", "~/Views/Shared/{0}.cshtml" };
        PartialViewLocationFormats = new string[] { "~/Views/{1}/{0}.cshtml", "~/Views/Shared/{0}.cshtml" };
        FileExtensions = new string[] { "cshtml" };
    }
}

Index.cshtml

@model IEnumerable<SifatWeb2016MVC.Models.Coletora.Atendimento>

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

    var isAdministradorTotal = User.IsAdministradorTotal();
    var isMultipleEmpresa = EmpresaHelper.IsMultipleEmpresa();
}

<div class="row wrapper border-bottom white-bg page-heading">
    <div class="col-sm-4">
        <h2>@ViewBag.Title</h2>
        <ol class="breadcrumb">
            <li class="active">
                <strong>Atendimentos</strong>
            </li>
        </ol>
    </div>
</div>

<div class="wrapper wrapper-content animated fadeInRight">
    @if (ViewBag.AtendimentoPesquisa == null)
    {
        Html.RenderPartial(MVC.Coletora.Atendimento.Views.IndexFilter, new SifatWeb2016MVC.Models.Coletora.AtendimentoPesquisa());
    }
    else
    {
        Html.RenderPartial(MVC.Coletora.Atendimento.Views.IndexFilter, (SifatWeb2016MVC.Models.Coletora.AtendimentoPesquisa)ViewBag.AtendimentoPesquisa);
    }
    <div class="row">
        <div class="col-lg-12">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h5>Lista de Atendimentos</h5>
                    <div class="ibox-tools">
                        @Html.ActionLink("Criar novo", MVC.Coletora.Atendimento.Create(), new { @class = "btn btn-primary btn-xs" })
                    </div>
                </div>
                <div class="ibox-content">
                    <table id="datatable" class="table table-striped table-bordered table-hover full-width">
                        <thead>
                            <tr>
                                <th>
                                    @Html.DisplayNameFor(model => model.AtendimentoId)
                                </th>
                                @if (isAdministradorTotal & isMultipleEmpresa)
                                {
                                    <th>
                                        @Html.DisplayNameFor(model => model.EmpresaId)
                                    </th>
                                }
                                <th>
                                    @Html.DisplayNameFor(model => model.PessoaId)
                                </th>
                                <th>
                                    @Html.DisplayNameFor(model => model.PessoaEnderecoId)
                                </th>
                                <th>
                                    @Html.DisplayNameFor(model => model.PessoaParaContato)
                                </th>
                                <th>
                                    @Html.DisplayNameFor(model => model.CaminhaoId)
                                </th>
                                <th>
                                    @Html.DisplayNameFor(model => model.CaminhoneiroId)
                                </th>
                                <th>
                                    @Html.DisplayNameFor(model => model.ColetoraId)
                                </th>
                                <th>
                                    @Html.DisplayNameFor(model => model.Valor)
                                </th>
                                <th>
                                    @Html.DisplayNameFor(model => model.DataEntrega)
                                </th>
                                <th>
                                    @Html.DisplayNameFor(model => model.DataDevolucao)
                                </th>
                                <th>
                                    @Html.DisplayNameFor(model => model.Pago)
                                </th>
                                <th>
                                    @Html.DisplayNameFor(model => model.Concluido)
                                </th>
                                <th class="no-sort no-print">
                                    Ações
                                </th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach (var item in Model)
                            {
                                <tr>
                                    <td>
                                        @item.AtendimentoId
                                    </td>
                                    @if (isAdministradorTotal & isMultipleEmpresa)
                                    {
                                        <td>
                                            @item.Empresa.RazaoSocial
                                        </td>
                                    }
                                    <td>
                                        @item.Pessoa.Nome
                                    </td>
                                    <td>
                                        @item.PessoaEndereco.EnderecoNumeroComplementoCEP
                                    </td>
                                    <td>
                                        @item.PessoaParaContato
                                    </td>
                                    <td>
                                        @item.Caminhao.Descricao
                                    </td>
                                    <td>
                                        @item.Caminhoneiro.Pessoa.Nome
                                    </td>
                                    <td>
                                        @item.Coletora.Descricao
                                    </td>
                                    <td>
                                        @item.ValorStringWithSymbol
                                    </td>
                                    <td>
                                        @item.DataEntregaString
                                    </td>
                                    <td>
                                        @item.DataHoraDevolucaoString
                                    </td>
                                    <td>
                                        @item.PagoSimNao
                                    </td>
                                    <td>
                                        @item.ConcluidoSimNao
                                    </td>
                                    <td>
                                        @Html.ActionLink("Detalhes", MVC.Coletora.Atendimento.Details().AddRouteValue("id", item.AtendimentoId), new { @class = "btn btn-primary btn-sm" })
                                        @Html.ActionLink("Editar", MVC.Coletora.Atendimento.Edit().AddRouteValue("id", item.AtendimentoId), new { @class = "btn btn-default btn-sm" })
                                        @Html.ActionLink("Excluir", MVC.Coletora.Atendimento.Delete().AddRouteValue("id", item.AtendimentoId), new { @class = "btn btn-default btn-sm" })
                                    </td>
                                </tr>
                            }
                        </tbody>
                        <tfoot>
                            <tr>
                                <th>
                                    @Html.DisplayNameFor(model => model.AtendimentoId)
                                </th>
                                @if (isAdministradorTotal & isMultipleEmpresa)
                                {
                                    <th>
                                        @Html.DisplayNameFor(model => model.EmpresaId)
                                    </th>
                                }
                                <th>
                                    @Html.DisplayNameFor(model => model.PessoaId)
                                </th>
                                <th>
                                    @Html.DisplayNameFor(model => model.PessoaEnderecoId)
                                </th>
                                <th>
                                    @Html.DisplayNameFor(model => model.PessoaParaContato)
                                </th>
                                <th>
                                    @Html.DisplayNameFor(model => model.CaminhaoId)
                                </th>
                                <th>
                                    @Html.DisplayNameFor(model => model.CaminhoneiroId)
                                </th>
                                <th>
                                    @Html.DisplayNameFor(model => model.ColetoraId)
                                </th>
                                <th>
                                    @Html.DisplayNameFor(model => model.Valor)
                                </th>
                                <th>
                                    @Html.DisplayNameFor(model => model.DataEntrega)
                                </th>
                                <th>
                                    @Html.DisplayNameFor(model => model.DataDevolucao)
                                </th>
                                <th>
                                    @Html.DisplayNameFor(model => model.Pago)
                                </th>
                                <th>
                                    @Html.DisplayNameFor(model => model.Concluido)
                                </th>
                                <th>
                                    Ações
                                </th>
                            </tr>
                        </tfoot>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>

@section Styles {
    @Styles.Render("~/Content/plugins/dataTables/dataTablesStyles")
    @Styles.Render("~/Content/bootstrapselect")
    @Styles.Render("~/Content/bootstraptoggle")
    @Styles.Render("~/plugins/sweetAlertStyles")
    @Styles.Render("~/plugins/toastrStyles")
    @Styles.Render("~/plugins/clockpickerStyles")
    @Styles.Render("~/plugins/dataPickerStyles")
}

@section Scripts {
    @Scripts.Render("~/plugins/dataTables")
    @Scripts.Render("~/bundles/bootstrapselect")
    @Scripts.Render("~/bundles/bootstraptoggle")
    @Scripts.Render("~/plugins/clockpicker")
    @Scripts.Render("~/plugins/dataPicker")
    @Scripts.Render("~/plugins/sweetAlert")
    @Scripts.Render("~/plugins/toastr")
    @Scripts.Render("~/bundles/maskmoney")
    @Scripts.Render("~/bundles/inputmask")

    @if (ViewBag.AtendimentoPesquisa == null)
    {
        Html.RenderPartial(MVC.Coletora.Atendimento.Views.IndexFilterScript, new ViewDataDictionary() { Model = null });
    }
    else
    {
        Html.RenderPartial(MVC.Coletora.Atendimento.Views.IndexFilterScript, (SifatWeb2016MVC.Models.Coletora.AtendimentoPesquisa)ViewBag.AtendimentoPesquisa);
    }

    @{ Html.RenderPartial(MVC.Shared.Views._BuscaPessoaEnderecoFilter, MVC.Coletora.Atendimento.GetPessoaEnderecos()); }
    @{ Html.RenderPartial(MVC.Coletora.Shared.Views._BuscaCaminhaoCaminhoneiroFilter, MVC.Coletora.Atendimento.GetCaminhaoCaminhoneiros()); }

    @{ Html.RenderPartial(MVC.Shared.Views._DataTables, new SifatWeb2016MVC.Models.DataTables() { ItensPagina = User.Identity.GetItensPagina(), Title = "Atendimentos", OutPutName = "atendimentos", SortColumn = 0, SortDirection = "desc" }); }
}

Controller - ActionResult - Index - MiniProfiler

var profiler = MiniProfiler.Current;

using (profiler.Step("Atendimentos - EF"))
{
    if (!string.IsNullOrWhiteSpace(id))
    {
        if (id.Equals("Filters"))
        {
            var userId = User.Identity.GetUserId();

            var atendimentoPesquisaConfiguracao = db.Coletoras_AtendimentosPesquisasConfiguracoes
                .AsNoTracking()
                .Where(model => model.UsuarioSistemaId.Equals(userId))
                .FirstOrDefault();

            AtendimentoPesquisa atendimentoPesquisa = null;

            if (atendimentoPesquisaConfiguracao != null && atendimentoPesquisaConfiguracao.AtendimentoPesquisaId.HasValue)
            {
                atendimentoPesquisa = db.Coletoras_AtendimentosPesquisas
                    .AsNoTracking()
                    .Where(model => model.UsuarioSistemaId.Equals(userId) & model.AtendimentoPesquisaId == atendimentoPesquisaConfiguracao.AtendimentoPesquisaId)
                    .FirstOrDefault();
            }

            if (atendimentoPesquisa != null)
            {
                return AtendimentosPesquisa(atendimentoPesquisa, false);
            }
        }
        else
        {
            return MVC.Error.BadRequest();
        }
    }

    ClearAtendimentoPesquisaConfiguracao();

    return Atendimentos();
}

0 个答案:

没有答案