Just-in-time compilation of Razor (* .cshtml) files in ASP.NET Core

1 minute read

Compiling Razor files in ASP.NET Core

Just-in-time compilation of web pages (.csthml) is not enabled by default in ASP.NET Core.
Razor is very powerful because it allows you to write C # code inline in HTML, but it can be very tedious to run every time you edit cshtml when adjusting layouts or debugging JavaScript.

In ASP.NET Core, you need to edit and save the .cshtml in the debug run state of Visual Studio, reload the page in the browser, and modify the code to reflect the changes.

This section describes the procedure for enabling run-time compilation of .cshtml only when running in the development environment.

As a side note, run-time in the development environment here refers to the case where the ASPNETCORE_ENVIRONMENT environment variable is set to Development. In Visual Studio, you can set the value of ASPNETCORE_ENVIRONMENT for each startup profile from [Properties]-[Debug] of the ASP.NET Core project. The default is set to Development.
ASPNETCORE_ENVIRONMENT

You can select which profile to use at startup from the debug run button.
Debug Button

Addition of RazorRuntimeCompilation package

Add RazorRuntimeCompilation to your ASP.NET Core project.

Select Solution Explorer-, right-click menu, select Manage NuGet Packages, select Browse, search for "RazorRuntimeCompilation", and search for "Microsoft.AspNetCore.Mvc.Razor". Install the .RuntimeCompilation "package. ![RazorRuntimeCompilation](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/172205/db8e4624-ee2f-7300-5247-1b26c1317694.png)

Edit Startup.cs

Edit Starup.cs. If “services.AddRazorPages ()” in the ConfigureServices method is set to “services.AddRazorPages (). AddRazorRuntimeCompilation ();”, just-in-time compilation is enabled, but this time, just-in-time compilation is performed only in the development environment. Make it valid.

When creating an instance of Startup class, add an argument to the constructor to save the instance of IWebHostEnvironment. It’s a dependency injection, so edit the existing constructor and add the arguments.

Startup.cs



//For saving an instance of IWebHostEnvironment
private IWebHostEnvironment _webHostEnvironment;

public Startup(IConfiguration configuration, IWebHostEnvironment env)
{
   Configuration = configuration;
   //Save an instance of IWebHostEnvironment
   this._webHostEnvironment = env;
}

Then edit the ConfigureServices method. Browse to the saved instance of IWebHostEnvironment and call AddRazorRuntimeCompilation so that just-in-time compilation is enabled only during debugging.

Startup.cs


public void ConfigureServices(IServiceCollection services)
{
    var mvcBuilder = services.AddRazorPages();

    if (this._webHostEnvironment.IsDevelopment())
    {
        //Enable just-in-time compilation
        mvcBuilder.AddRazorRuntimeCompilation();
    }

Now, the run-time compilation of the .cshtml file is valid only when it is run in the development environment.

Just-in-time compilation of Razor files is very convenient during development, so please take advantage of it.

Reference site