Enable Razor Runtime Compilation

Introduction

Suppose you have been using ASP.NET MVC version 5.x for quite some time now and started to understand ASP.NET Core. In that case, I tell you you’re doing an excellent job because you’re upgrading your skills set to the latest version of ASP.NET Core.

Have you ever wondered that we can enable runtime compilation of Razor files? Isn’t that exciting? I hope you’re excited because, in this tutorial, we’ll see how we can set up the runtime compilation.

Background

For both ASP.NET Core and ASP.NET Core MVC project, a Razor file having a .cshtml extension are compiled at both build and publish time. Furthermore, runtime compilation is optionally enabled, and this can entirely be enabled if you have configured your project correctly.

This means you can allow runtime compilation, giving you the ability to see any modified UI changes in real-time without ever restarting your application.

Sounds good? Let’s see how we can do that.

Enabling Razor Runtime Compilation

For Existing Projects of ASP.NET Core and ASP.NET Core MVC

Suppose you have a preexisting ASP.NET CORE project. In that case, you can directly install the package “Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation” via NuGet.

Note: If you’re going to install this package, if you’re using ASP.NET Core 3.x, choose version 3.x.x, and if you’re using ASP.NET Core 5.x, select version 5.x.x.

Add the following code to your services.

Set this way if your project is ASP.NET Core.

  // This method gets called by the runtime. Use this method to add services to the container.
  public void ConfigureServices(IServiceCollection services)
  {
      services.AddRazorPages().AddRazorRuntimeCompilation();
  }

Set this way if your project is ASP.NET Core MVC

 // This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
    services.AddControllersWithViews().AddRazorRuntimeCompilation();
}

When Creating a New Project

You just need to check “Enable Razor runtime compilation” when you’re creating a new project.  

Moreover, this option is available when choosing the target framework starting from version 3.1 to the current version 5.x.

See sample screenshot below.

Remember these when you are creating a new ASP.NET Core or ASP.NET Core MVC projects.

  • The package is already installed for you when you selected or checked the “Enable Razor runtime compilation” when creating a new project.
  • You need to set and enable the runtime compilation at the Startup.cs file.

Enhance the Code!

I know that we need to enhance the code because we don’t want these services to be running on staging or production.

Enhancing or refactoring the code is relatively easy.

See the samples below.

The code below is for the ASP.NET Core project.

public Startup(IConfiguration configuration, IWebHostEnvironment env)
{
    Configuration = configuration;
    Env = env;
}

public IConfiguration Configuration { get; }

public IWebHostEnvironment Env { get; }


// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
    var builder = services.AddRazorPages();

    if (Env.IsDevelopment()) {

        builder.AddRazorRuntimeCompilation();
    }
}

The code below is for the ASP.NET Core MVC project.

public Startup(IConfiguration configuration, IWebHostEnvironment env)
{
    Configuration = configuration;
    Env = env;
}

public IConfiguration Configuration { get; }
public IWebHostEnvironment Env { get; }

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
    var builder = services.AddControllersWithViews();

    if (Env.IsDevelopment())
    {

        builder.AddRazorRuntimeCompilation();
    }
}

Summary

This article tutorial has shown us how to enable the Razor runtime compilation on both ASP.NET Core and ASP.NET Core MVC projects.

Moreover, we have shown how we can make the code versatile because we don’t want the runtime compilation run on the production server.

Once again, I hope you have enjoyed this article/tutorial as I have enjoyed writing it.

Stay tuned for more (keep visiting our blog and share this with your friends, colleagues, and network).

Until next time, happy programming!

Please don’t forget to bookmark, like, and comment. Cheers! And Thank you!