问题描述
昨天我更新到net core 2.1.
Yesterday I updated to net core 2.1.
现在,如果我正在调试,视图会被预编译,这在启动过程中当然需要很长时间......是否可以退回到以前的行为,如果需要,视图会及时编译?
Now if I am debugging, the views getting precompiled, which ofcourse takes a long time during startup... Is it possible to fall back to the previous behavior, where the Views are compiled just in time, if it is needed?
我的 csproj 中没有与预编译相关的参考资料.它是来自元包的东西吗?
I have no reference related to precompilation in my csproj. Is it something that comes from the meta package?
<ItemGroup>
<PackageReference Include="JetBrains.Annotations" Version="11.1.0" />
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.1.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.1.0" PrivateAssets="All" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="2.5.0" />
<!--<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.ViewCompilation" Version="2.0.0" PrivateAssets="All" />-->
</ItemGroup>
推荐答案
.net core >= 3 (也叫.net 5)
Microsoft 创建了一个 Nuget 包.这是此处记录.
只需参考 Microsoft.AspNetCore.Mvc.Razor.有条件地在 .csproj 文件中添加 RuntimeCompilation
.不要忘记调整版本,你实际使用的.
Just reference Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation
in your .csproj file conditionally. Don't forget to adjust the version, you actualy use.
<PackageReference
Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation"
Version="3.1.0"
Condition="'$(Configuration)' == 'Debug'" />
同时配置您的服务
public void ConfigureServices(IServiceCollection services)
{
// your MVC Builder (AddRazorPages/AddControllersWithViews)
IMvcBuilder builder = services.AddRazorPages();
#if DEBUG
// Only use Runtime Compilation on Debug
if (Env.IsDevelopment())
{
builder.AddRazorRuntimeCompilation();
}
#endif
}
当然,当你想通用运行时编译时,即使发布,你也不需要所有的条件.
Ofcourse, when you want to general use Runtime Compilation, even when published, you don't need all the conditions.
这可以使用属性 .csproj 文件中的 -razor-sdk" rel="noreferrer">RazorCompileOnBuild
:
This can be accomplished using the property RazorCompileOnBuild
in the .csproj file:
<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<RazorCompileOnBuild>false</RazorCompileOnBuild>
<RazorCompileOnPublish>true</RazorCompileOnPublish>
</PropertyGroup>
这样 Razor 文件仅在发布期间进行预编译.
This way the Razor files are only precompiled during publishing.
根据用例,您还想根据构建配置进行配置:
Depending on the usecase you also want to configure this depending on the build configuration:
<PropertyGroup Condition="'$(Configuration)' == 'Debug'">
<RazorCompileOnBuild>false</RazorCompileOnBuild>
<RazorCompileOnPublish>true</RazorCompileOnPublish>
</PropertyGroup>
这篇关于如何禁用 net core 2.1+/net 5 中的预编译视图进行调试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!