简体   繁体   English

查看asp.net mvc和asp.net core mvc的目录编译时间差

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

im extremely curious about exceptional situation between asp.net mvc (4.7.2) and asp.net core mvc (2.2).我对 asp.net mvc (4.7.2) 和 asp.net core mvc (2.2) 之间的异常情况非常好奇。

What im doing is:我正在做的是:

Create empty asp.net mvc project, add new "TestController" cloned from default "HomeController", duplicate views of home controller and name View directory as "Test".创建空的 asp.net mvc 项目,添加从默认“HomeController”克隆的新“TestController”,复制主 controller 的视图并将 View 目录命名为“Test”。

doing exactly the same on asp.net core mvc project.在 asp.net 核心 mvc 项目上做同样的事情。

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.运行 asp.net mvc 项目并将 url 更改为 /Test/Index(视图与 Home/Index 完全相同),第一次运行大约需要 300 毫秒。 if i refresh the page it took ~5-6 milliseconds to run.如果我刷新页面,运行大约需要 5-6 毫秒。

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.我在 asp.net 核心 mvc 项目上做的完全一样,第一次运行大约需要 30 毫秒,每次刷新大约需要 5-6 毫秒。

i tested several cases and i think its all about "First creation of X type of controller".我测试了几个案例,我认为这都是关于“第一次创建 X 类型的控制器”。

im working on asp.net on 5-6 years but i've noticed this now.我在 asp.net 上工作了 5-6 年,但我现在注意到了这一点。 you can say it's no problem just 300 ms, but on the low resourced machine it took ~30 seconds.您可以说只需 300 毫秒就没有问题,但在资源不足的机器上需要大约 30 秒。

what is this all about and how can i reduce this time to asp.net core time (~30ms)?这是怎么回事?我怎样才能将这个时间减少到 asp.net 核心时间(~30ms)?

EDIT: it turns out its not about the creation of controller.编辑:事实证明它与 controller 的创建无关。 It's about the directories of views.这是关于视图的目录。 I changed the Index action of TestController's view path with "~/Views/Home/Index" it opened fast (if i rendered any view from this folder before), but if i "first time render a view from X directory (or sub directory) it works slowly as i mentioned."我用“~/Views/Home/Index”更改了TestController的视图路径的索引操作,它打开得很快(如果我之前从这个文件夹渲染了任何视图),但是如果我“第一次从X目录(或子目录)渲染视图) 正如我所提到的,它运行缓慢。”

i think its all about compiling views (or view directories).我认为这一切都与编译视图(或视图目录)有关。 but im not figure it out yet.但我还没有弄清楚。

consequently, if i move all my view files to same directory, this case never happens.因此,如果我将所有视图文件移动到同一个目录,这种情况永远不会发生。 but it will be very confusing and not feel right approach.但这会很混乱,感觉不是正确的方法。

I finally figured it out.我终于弄明白了。 It's all about compiling views at starting the application (not at runtime).这都是关于在启动应用程序时编译视图(而不是在运行时)。

And its speeding up your website (especially if you have too many views)它可以加快您的网站速度(尤其是如果您的浏览量过多)

Solution:解决方案:

  1. Unload project卸载项目
  2. Right click on the unloaded project and go > "Edit *.csproj"右键单击卸载的项目和 go >“编辑 *.csproj”
  3. Paste these settings to bottom line of the file将这些设置粘贴到文件的最后一行

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

  4. Reload the project重新加载项目

  5. Paste this code to global asax > Application_Start将此代码粘贴到全局 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. Run the project运行项目

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM