【问题标题】:Razor in asp.net core is very slowasp.net core中的Razor很慢
【发布时间】:2016-10-10 01:33:33
【问题描述】:

我创建了一个小的 asp.net core mvc 项目来测试 asp.net 应用程序在 Linux (Ubuntu 16.04) 上的速度。

我创建了与 AspUser 类(存储在 PostgreSQL 数据库中)一起使用的 CRUD 控制器。当我调用显示存储用户列表的 index 方法时,剃刀渲染非常慢。渲染内容需要 2 秒(这不是第一次调用 - 第一次调用需要 8 秒)。数据库中有 1000 个用户。

wrk 实用程序 wrk -c 256 -t 32 -d 10 http://localhost:5000/aspusers 显示每秒 2,6 个请求。 我在 nodejs 中做了同样的例子,wrk 实用程序每秒显示 20 个请求。

硬件:Amd FX 8150 8 核,3.6 GHz,8 GB RAM

有人知道为什么视图渲染这么慢吗?

型号:

[Table("asp_users")]
public class AspUser
{
    [Column("id")]
    public long Id { get; set; }

    [Column("first_name")]
    [Required]
    public string FirstName { get; set; }

    [Column("last_name")]
    public string LastName { get; set; }

    [Column("age")]
    public int Age { get; set; }

    [Column("created_at")]
    public DateTime CreatedAt { get; set; }

    [Column("updated_at")]
    public DateTime UpdatedAt { get; set; }
}

控制器:

public class AspUsersController : Controller
{
    public async Task<IActionResult> Index()
    {
        var users = await _context.AspUsers.OrderBy(a => a.Age).ToListAsync();
        return View(users);
    }
}

查看:

@model IEnumerable<PostgresTest.Models.AspUser>
@{
    ViewData["Title"] = "Index";
}

<h2>Index</h2>
<p>
    <a asp-action="Create">Create New</a>
</p>
<table class="table">
    <thead>
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Age)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.FirstName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.LastName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.CreatedAt)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.UpdatedAt)
        </th>
        <th></th>
    </tr>
    </thead>
    <tbody>
        @foreach (var item in Model)
        {
            <tr>
                <td>
                    @item.Age
                </td>
                <td>
                    @item.FirstName
                </td>
                <td>
                    @item.LastName
                </td>
                <td>
                    @item.CreatedAt
                </td>
                <td>
                    @item.UpdatedAt
                </td>
                <td>
                    <a asp-action="Edit" asp-route-id="@item.Id">Edit</a> |
                    <a asp-action="Details" asp-route-id="@item.Id">Details</a> |
                    <a asp-action="Delete" asp-route-id="@item.Id">Delete</a>
                </td>
            </tr>
        }
    </tbody>
</table>

【问题讨论】:

  • 在开发机器(Win7 x64,dotnet core tools + SDK)上足够有趣,它运行得非常快,但在 Win Server 2012R2(只安装了 DotNetCore.1.0.1-WindowsHosting)上它非常慢。

标签: asp.net-mvc razor


【解决方案1】:

您可以为数据库和 ViewModel 中的数据创建单独的模型

例如:

视图模型

public class ViewAspUser
{

    public long Id { get; set; }

    [Required]
    public string FirstName { get; set; }

    [Required]
    public string LastName { get; set; }

    [Required]
    public int Age { get; set; }

    [Required]
    public DateTime CreatedAt { get; set; }

    [Required]
    public DateTime UpdatedAt { get; set; }
}

数据类

public class DataAspUser
{
    [Column("id")]
    public long Id { get; set; }

    [Column("first_name")]
    public string FirstName { get; set; }

    [Column("last_name")]
    public string LastName { get; set; }

    [Column("age")]
    public int Age { get; set; }

    [Column("created_at")]
    public DateTime CreatedAt { get; set; }

    [Column("updated_at")]
    public DateTime UpdatedAt { get; set; }
}

这会起作用,因为我遇到了同样的问题。最后我把类分开了。这是因为如果你使用带有数据注释的模型,每次在视图中访问你的剃须刀时,它都会连接到数据库。

【讨论】:

  • 如果你用 list() 就不行
  • 我什至做到了。不过,我觉得页面回发很慢。如果你使用ajax实现。这该死的更快!
猜你喜欢
  • 2021-07-15
  • 1970-01-01
  • 2019-10-14
  • 2018-10-29
  • 2018-02-24
  • 2016-12-01
  • 1970-01-01
  • 2018-04-03
  • 2020-07-19
相关资源
最近更新 更多