Menu Close

Razor file compilation in ASP.NET Core

In this article we discuss about Razor file compilation in ASP.NET Core. Before starting this article please visit our previous article How controller finds a view (View Discovery). To learn step by step process about ASP.NET Core from beginner to advance level then please follow the link.

What is Razor Compilation ?

Razor files (.cshtml) are compiled at both build and publish time and this gives better performance as your views are compiled. We can also enable runtime compilation, which will help developers to see any modified view change in real-time, without starting the application again.

 In earlier versions of ASP.NET Core, Razor compilation was enabled by default. From .NET Core 3.x, it is on demand. 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. This post will help you to enable Razor file compilation in development environment.

In Simply term you can say Razor compilation’s come into picture when we want to refresh the view page to modifying some text. As in ASP.NET Core 3.x razor page are compiled at both build and publish time to gives better performance, but as a developer we need to look the change in view page without build and run the application again and again.

When Razor file compilation takes place ?

By default, Rqazor file compilation takes place in 2 ways.

  • Build – Application is build to collect the changes of .cs page and razor page
  • Publish – Application is publish to upload into staging/production server.

How to enable Razor file compilation ?

To enable razor file compilation we need to install the below package using Nuget.


Then we need to add below code in startup.cs file under configure service method


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

public IWebHostEnvironment WebHostEnvironment { get; }
        public Startup(IWebHostEnvironment webHostEnvironment)
            WebHostEnvironment = webHostEnvironment;
       public void ConfigureServices(IServiceCollection services)
#if DEBUG 
            if (WebHostEnvironment.IsDevelopment())

On above we declared Startup constructor, where the IWebHostEnvironment will be injected and sets the WebHostEnvironment property. And if the environment mode is set as production in environment variables then our above method is see if the environment mode is Development then it should enable Razor compilation.


Here Environment Variable is set as Development so Razor compilation takes place, if it is on staging/production mode then Razor compilations not enable.

All are set, now you can see that if anything change in view page it reflect suddenly without build again and again.


Leave behind your valuable queries and suggestions in the comment section below. Also, if you think this article helps you, do not forget to share this with your developer community. Happy Coding 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *