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:
-
Installation Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation the NuGet package.
-
Fresh entry
Startup.ConfigureServices
method to compriseAddRazorRuntimeCompilation
call. 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 Configuration
values, 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.ConfigureServices
method to comprise AddRazorRuntimeCompilation
call. Conditionally executed AddRazorRuntimeCompilation
, so that only when the ASPNETCORE_ENVIRONMENT
variable is set Development
when 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 }