https://www.cnblogs.com/yyfh/archive/2020/02/27/12373249.htmlhtml
0x01 前言
ASP.NET Core在默認發佈狀況下,會啓動預編譯將試圖編譯成xx.Views.dll,也許在視圖中打算修改一處很細小的地方咱們須要再從新編譯視圖進行發佈。下面我將從 ASP.NET Core 3 以前版本到 ASP.NET Core 3X 以後版本的一個配置列下下方供你們參考。bash
0x02 預編譯
在ASP.Core中默認是爲咱們啓動的預編譯,默認系統會將視圖編譯進行預編譯處理,最終會將編譯好的視圖生成一個DLL xx.Views.dll.mvc
0x03 動態編譯
動態編譯也就是當咱們視圖文件發生變化時進行從新編譯和更新Razor文件app
ASP.NET Core 3以前版本:ui
<PropertyGroup> <TargetFramework>netcoreapp2.2</TargetFramework> <AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel> <RazorCompileOnBuild>false</RazorCompileOnBuild> <MvcRazorCompileOnPublish>false</MvcRazorCompileOnPublish> </PropertyGroup>
經過上述代碼只能幫咱們作到在開發環境中進行動態編譯,那麼咱們須要加上以下代碼來實現咱們生產環境中動態編譯.spa
public void ConfigureServices(IServiceCollection services) { services.AddMvc().AddRazorOptions(options => options.AllowRecompilingViewsOnFileChange = true); }
條件:調試
ASP.NET Core 3.X版本:code
安裝NuGet包htm
Install-Package Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation
public void ConfigureServices(IServiceCollection services) { services.AddRazorPages() .AddRazorRuntimeCompilation(); }
<PropertyGroup> <TargetFramework>netcoreapp3.1</TargetFramework> <RazorCompileOnBuild>false</RazorCompileOnBuild> <MvcRazorCompileOnPublish>false</MvcRazorCompileOnPublish> </PropertyGroup>
經過上述代碼咱們能夠在生產環境中也能夠進行動態編譯,固然咱們能夠經過條件來限制.blog
基於環境和配置模式啓用運行時編譯:
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="3.1.2" Condition="'$(Configuration)' == 'Debug'" />
public IWebHostEnvironment Env { get; set; } public void ConfigureServices(IServiceCollection services) { IMvcBuilder builder = services.AddRazorPages(); #if DEBUG if (Env.IsDevelopment()) { builder.AddRazorRuntimeCompilation(); } #endif }
Reference
https://docs.microsoft.com/en-us/aspnet/core/mvc/views/view-compilation?view=aspnetcore-3.1