Posted by Anuraj on Monday, February 10, 2020 Reading time :1 minute
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
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
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 :)
What do you think? I would like to hear your thoughts, suggestions, and questions in the comments section below.