Solve .NET CORE3.0 MVC view layer is not time compilation

Microsoft's official documentation

Razor compiler

Razor SDK by default when the compiler when generating Razor-enabled files and publishing. When enabled, the runtime compiler will generate a compile time to add, update permit Razor file (if edit).

Compile and run time

Enable runtime environment and compiler for all configuration modes:

  1. Installation Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation the NuGet package.

  2. Fresh entry Startup.ConfigureServicesmethod to comprise AddRazorRuntimeCompilationcall. E.g:

public void ConfigureServices(IServiceCollection services)
{
    services.AddRazorPages()
        .AddRazorRuntimeCompilation();

    // code omitted for brevity
}

 

Runtime compiled conditionally enabled

It can only be used for local development during run-time compiler enabled. In this way ensures that the output is enabled conditionally published:

  • Use the compiler view.
  • Small.
  • File Watcher is not enabled in a production environment.

Enable runtime compiled based on environment and configuration mode:

The activity Configurationvalues, conditionally reference Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation package:

<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="3.1.0" Condition="'$(Configuration)' == 'Debug'" />

 Fresh entry Startup.ConfigureServicesmethod to comprise AddRazorRuntimeCompilationcall. Conditionally executed AddRazorRuntimeCompilation, so that only when the ASPNETCORE_ENVIRONMENTvariable is set Developmentwhen running in debug mode:

public IWebHostEnvironment Env { get; set; }

public void ConfigureServices(IServiceCollection services)
{
    IMvcBuilder builder = services.AddRazorPages();

#if DEBUG
    if (Env.IsDevelopment())
    {
        builder.AddRazorRuntimeCompilation();
    }
#endif

    // code omitted for brevity
}

 

Guess you like

Origin www.cnblogs.com/yunspider/p/12149131.html