Razor file compilation in ASP.NET Core

February 10, 2020 by Anuraj

aspnetcore Razor

This post is about enabling Razor file compilation in ASP.NET Core. In earlier versions of ASP.NET Core, Razor compilation was enabled by default. From .NET Core 3.x, it is on demand. This post will help you to enable Razor file compilation in development environment.

As mentioned earlier, when you edit as razor file, save and refresh the page - with latest version of .NET Core and Visual Studio it will not load the latest changes. To load the latest changes you need to stop the debugging and restart.

You can fix this by adding Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation nuget package to your project. Please include the Condition expression, so that it is included only in the debug build, like this.

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

And you need to add the following code in the ConfigureServices method.

public void ConfigureServices(IServiceCollection services)
{
    services.AddControllersWithViews()
        .AddRazorRuntimeCompilation();
}

You can configure the app to enable Razor Runtime Compilation only in Debug environment using Debug environment variable, like this.

public IWebHostEnvironment WebHostEnvironment { get; set; }
public void ConfigureServices(IServiceCollection services)
{
    var builder = services.AddControllersWithViews();
#if DEBUG
    if (WebHostEnvironment.IsDevelopment())
    {
        builder.AddRazorRuntimeCompilation();
    }
#endif
}

Here is the Startup constructor, where the IWebHostEnvironment will be injected and sets the WebHostEnvironment property.

public Startup(IConfiguration configuration, 
    IWebHostEnvironment webHostEnvironment)
{
    Configuration = configuration;
    WebHostEnvironment = webHostEnvironment;
}

Razor files with a .cshtml extension are compiled at both build and publish time using the Razor SDK. If Run time compilation is enabled, Razor files will be updated if they are edited and saved.

Happy Programming :)

Copyright © 2024 Anuraj. Blog content licensed under the Creative Commons CC BY 2.5 | Unless otherwise stated or granted, code samples licensed under the MIT license. This is a personal blog. The opinions expressed here represent my own and not those of my employer. Powered by Jekyll. Hosted with ❤ by GitHub