查看asp.netmvc与asp.netcore mvc的目录编译时间差异

View directory compilation time difference between asp.net mvc and asp.net core mvc

我非常好奇 asp.net mvc (4.7.2) 和 asp.net core mvc (2.2) 之间的异常情况。

我正在做的是:

Create empty asp.net mvc project , add new "TestController" cloned from default "HomeController" , duplicate views of home controller and name View directory as "Test".

doing exactly the same on asp.net core mvc project.

run asp.net mvc project and change url as /Test/Index (the view is exactly the same as Home/Index) and it took ~300ms to run on first time. if i refresh the page it took ~5-6 milliseconds to run.

im doing exactly the same on asp.net core mvc project and it took ~30 ms on first run and ~5-6 ms on every time i refresh.

我测试了几个案例,我认为这都是关于“首次创建 X 类型控制器”的。

我在 asp.net 上工作了 5-6 年,但我现在注意到了这一点。你可以说这只是 300 毫秒没问题,但在资源不足的机器上它花了 ~30 秒。

这是怎么回事,我怎样才能将这个时间减少到 asp.net 核心时间(~30 毫秒)?

编辑: 事实证明这与控制器的创建无关。这是关于视图的目录。我用“~/Views/Home/Index”更改了 TestController 视图路径的索引操作,它打开速度很快(如果我之前从该文件夹渲染过任何视图),但是如果我 ”第一次从 X 目录渲染视图(或子目录)正如我提到的那样,它运行缓慢。"

我认为这都是关于编译视图(或视图目录)的。但我还没弄明白。

因此,如果我将所有视图文件移动到同一目录,这种情况就不会发生。但这样会很混乱,而且感觉不对。

我终于想通了。 这都是关于在启动应用程序时(而不是在运行时)编译视图。

它可以加快您的网站速度(尤其是当您的浏览量过多时)

解法:

  1. 卸载项目
  2. 右键单击卸载的项目并转到 > "Edit *.csproj"
  3. 将这些设置粘贴到文件的最后一行

    <Target Name="BeforeBuild"> <!-- Remove obj folder --> <RemoveDir Directories="$(BaseIntermediateOutputPath)" /> <!-- Remove bin folder --> <RemoveDir Directories="$(BaseOutputPath)" /> </Target>

  4. 重新加载项目

  5. 将此代码粘贴到全局 asax > Application_Start

    var siteId = "1"; // you can find your own from applicationhost.config
    var appVirtualDir = $"/LM/W3SVC/{siteId}/ROOT";
    var clientBuildManager = new ClientBuildManager(appVirtualDir, null, null,
                                new ClientBuildManagerParameter
                                {
                                    PrecompilationFlags = PrecompilationFlags.Default,
                                });
    clientBuildManager.PrecompileApplication();
    
  6. 运行项目