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

}

 

posted @ 2020-10-18 13:22  一生有你.Net  阅读(264)  评论(0编辑  收藏