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.
  1. // This method gets called by the runtime. Use this method to add services to the container.  
  2. public void ConfigureServices(IServiceCollection services)  
  3. {  
  4.     services.AddRazorPages().AddRazorRuntimeCompilation();  
  5. }  
 Set this way if your project is ASP.NET Core MVC
  1. // This method gets called by the runtime. Use this method to add services to the container.  
  2. public void ConfigureServices(IServiceCollection services)  
  3. {  
  4.     services.AddControllersWithViews().AddRazorRuntimeCompilation();  
  5. }  
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. 
 
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.
  1. public Startup(IConfiguration configuration, IWebHostEnvironment env)  
  2. {  
  3.     Configuration = configuration;  
  4.     Env = env;  
  5. }  
  6.    
  7. public IConfiguration Configuration { get; }  
  8.    
  9. public IWebHostEnvironment Env { get; }  
  10.    
  11.    
  12. // This method gets called by the runtime. Use this method to add services to the container.  
  13. public void ConfigureServices(IServiceCollection services)  
  14. {  
  15.     var builder = services.AddRazorPages();  
  16.    
  17.     if (Env.IsDevelopment()) {  
  18.    
  19.         builder.AddRazorRuntimeCompilation();  
  20.     }  
  21. }  
The code below is for the ASP.NET Core MVC project.
  1. public Startup(IConfiguration configuration, IWebHostEnvironment env)  
  2. {  
  3.     Configuration = configuration;  
  4.     Env = env;  
  5. }  
  6.    
  7. public IConfiguration Configuration { get; }  
  8. public IWebHostEnvironment Env { get; }  
  9.    
  10. // This method gets called by the runtime. Use this method to add services to the container.  
  11. public void ConfigureServices(IServiceCollection services)  
  12. {  
  13.     var builder = services.AddControllersWithViews();  
  14.    
  15.     if (Env.IsDevelopment())  
  16.     {  
  17.    
  18.         builder.AddRazorRuntimeCompilation();  
  19.     }  
  20. }  

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. This article was originally written and posted here.
 
Stay tuned for more. Until next time, happy programming!
 
Please don’t forget to bookmark, like, and comment. Cheers! And Thank you!