ASP.NET Core 3.1 Razor 视图预编译、动态编译
1.安装NuGet包
Install-Package Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation
2.Startup.cs 配置
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages() .AddRazorRuntimeCompilation();
}
<PropertyGroup> <TargetFramework>netcoreapp3.1</TargetFramework>
<RazorCompileOnBuild>false</RazorCompileOnBuild>
<MvcRazorCompileOnPublish>false</MvcRazorCompileOnPublish>
</PropertyGroup>
通过上述代码我们可以在生产环境中也可以进行动态编译,当然我们可以通过条件来限制.
public IWebHostEnvironment Env { get; set; }
public void ConfigureServices(IServiceCollection services)
{
IMvcBuilder builder = services.AddRazorPages();
#if DEBUG
if (Env.IsDevelopment())
{
builder.AddRazorRuntimeCompilation();
}
#endif
}