Razor file compilation in ASP.NET Core

By Rick Anderson

A Razor file is compiled at runtime, when the associated MVC view is invoked. Build-time Razor file publishing is unsupported. Razor files can optionally be compiled at publish time and deployed with the app—using the precompilation tool.

A Razor file is compiled at runtime, when the associated Razor Page or MVC view is invoked. Build-time Razor file publishing is unsupported. Razor files can optionally be compiled at publish time and deployed with the app—using the precompilation tool.

A Razor file is compiled at runtime, when the associated Razor Page or MVC view is invoked. Razor files are compiled at both build and publish time using the Razor SDK.

Razor files with a .cshtml extension are compiled at both build and publish time using the Razor SDK. Runtime compilation may be optionally enabled by configuring your application.

Razor compilation

Build- and publish-time compilation of Razor files is enabled by default by the Razor SDK. When enabled, runtime compilation complements build-time compilation, allowing Razor files to be updated if they are edited.

Build- and publish-time compilation of Razor files is enabled by default by the Razor SDK. Editing Razor files after they're updated is supported at build time. By default, only the compiled Views.dll and no .cshtml files or references assemblies required to compile Razor files are deployed with your app.

Important

The precompilation tool has been deprecated, and will be removed in ASP.NET Core 3.0. We recommend migrating to Razor Sdk.

The Razor SDK is effective only when no precompilation-specific properties are set in the project file. For instance, setting the .csproj file's MvcRazorCompileOnPublish property to true disables the Razor SDK.

If your project targets .NET Framework, install the Microsoft.AspNetCore.Mvc.Razor.ViewCompilation NuGet package:

<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.ViewCompilation"
                  Version="2.0.4"
                  PrivateAssets="All" />

If your project targets .NET Core, no changes are necessary.

The ASP.NET Core 2.x project templates implicitly set the MvcRazorCompileOnPublish property to true by default. Consequently, this element can be safely removed from the .csproj file.

Important

The precompilation tool has been deprecated, and will be removed in ASP.NET Core 3.0. We recommend migrating to Razor Sdk.

Razor file precompilation is unavailable when performing a self-contained deployment (SCD) in ASP.NET Core 2.0.

Set the MvcRazorCompileOnPublish property to true, and install the Microsoft.AspNetCore.Mvc.Razor.ViewCompilation NuGet package. The following .csproj sample highlights these settings:

<Project Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <TargetFramework>netcoreapp1.1</TargetFramework>
    <MvcRazorCompileOnPublish>true</MvcRazorCompileOnPublish>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore" Version="1.1.0" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.0" />
    <PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="1.1.0" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.ViewCompilation" Version="1.1.0-*" />
  </ItemGroup>
</Project>

Prepare the app for a framework-dependent deployment with the .NET Core CLI publish command. For example, execute the following command at the project root:

dotnet publish -c Release

A <project_name>.PrecompiledViews.dll file, containing the compiled Razor files, is produced when precompilation succeeds. For example, the screenshot below depicts the contents of Index.cshtml within WebApplication1.PrecompiledViews.dll:

Razor views inside DLL

Runtime compilation

Build-time compilation is supplemented by runtime compilation of Razor files. ASP.NET Core MVC will recompile Razor files when the contents of a .cshtml file change.

Build-time compilation is supplemented by runtime compilation of Razor files. The RazorViewEngineOptions AllowRecompilingViewsOnFileChange gets or sets a value that determines if Razor files (Razor views and Razor Pages) are recompiled and updated if files change on disk.

The default value is true for:

For guidance and examples of setting the app's compatibility version, see Compatibility version for ASP.NET Core MVC.

To enable runtime compilation for all environments and configuration modes:

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

  2. Update the project's Startup.ConfigureServices method to include a call to AddRazorRuntimeCompilation. For example:

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddRazorPages()
            .AddRazorRuntimeCompilation();
    
        // code omitted for brevity
    }
    

Conditionally enable runtime compilation

Runtime compilation can be enabled such that it's only available for local development. Conditionally enabling in this manner ensures that the published output:

  • Uses compiled views.
  • Is smaller in size.
  • Doesn't enable file watchers in production.

To enable runtime compilation based on the environment and configuration mode:

  1. Conditionally reference the Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation package based on the active Configuration value:

    <PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="3.1.0" Condition="'$(Configuration)' == 'Debug'" />
    
  2. Update the project's Startup.ConfigureServices method to include a call to AddRazorRuntimeCompilation. Conditionally execute AddRazorRuntimeCompilation such that it only runs in Debug mode when the ASPNETCORE_ENVIRONMENT variable is set to Development:

    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
    }
    

Additional resources