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.
And you need to add the following code in the ConfigureServices
method.
You can configure the app to enable Razor Runtime Compilation
only in Debug environment using Debug environment variable, like this.
Here is the Startup
constructor, where the IWebHostEnvironment
will be injected and sets the WebHostEnvironment
property.
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