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

标签 c# asp.net asp.net-mvc asp.net-mvc-4 asp.net-core

我非常好奇 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 。

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

解决方案:

  1. 卸载项目
  2. 右键单击卸载的项目并转到>“编辑*.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. 运行项目

关于c# - 查看asp.net mvc和asp.net core mvc的目录编译时间差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58900538/

相关文章:

c# - Active Directory 登录后,ASP.NET MVC 应用程序重定向到本地主机而不是应用程序 URI

c# - C#中指针的性能和使用

javascript - 为什么动态添加的 View 输入字段无法在主视图上附加 jQuery 事件

c# - 如何将枚举绑定(bind)到 WPF 中组合框的依赖属性?

javascript - 当注册达到 0 时显示消息

c# - 我想知道如何将值传递给另一个 View 并在单击 asp.net mvc 中的更新按钮时更新我的​​详细信息

javascript - 如何检测浏览器窗口/选项卡关闭事件?

css - 如何设置 ASP.NET MVC 5 使用 dotless/LESS

javascript - 如何计算添加元素?

c# - 查找最接近的 "nice"标尺图 block 宽度