我非常好奇 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 类型 Controller ”。
我在 asp.net 上工作了 5-6 年,但我现在注意到了这一点。你可以说这只是 300 毫秒没问题,但在资源不足的机器上它花了大约 30 秒。
这是怎么回事,我怎样才能将这个时间减少到 asp.net 核心时间(~30 毫秒)?
编辑: 事实证明这与 Controller 的创建无关。这是关于 View 的目录。我用“~/Views/Home/Index”更改了 TestController View 路径的索引操作,它打开速度很快(如果我之前从该文件夹渲染过任何 View ),但是如果我 “第一次从 X 目录渲染 View (或子目录),正如我提到的那样,它运行缓慢。”
我认为这全都与编译 View (或 View 目录)有关。但我还没有弄明白。
因此,如果我将所有 View 文件移动到同一目录,这种情况就不会发生。但这会非常困惑,而且感觉不正确。
最佳答案
我终于明白了。 这都是关于在启动应用程序时(而不是在运行时)编译 View 。
它会加快您的网站速度(尤其是当您的浏览量过多时)
解决方案:
- 卸载项目
- 右键单击卸载的项目并转到>“编辑*.csproj”
将这些设置粘贴到文件的底部
<Target Name="BeforeBuild"> <!-- Remove obj folder --> <RemoveDir Directories="$(BaseIntermediateOutputPath)" /> <!-- Remove bin folder --> <RemoveDir Directories="$(BaseOutputPath)" /> </Target>
重新加载项目
将此代码粘贴到全局 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();
运行项目
关于c# - 查看asp.net mvc和asp.net core mvc的目录编译时间差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58900538/