Skip to main content

ASP.NET Core 5 – Enabling Razor runtime 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. The recent version of ASP.NET Core (ASP.NET Core 5.0) Preview 2 came out and you can start building an app on ASP.NET Core 5.0 framework. ASP.NET Core 5.0 changes this experience for enabling razor runtime compilation. This is now available as an option while creating the ASP.NET Core 5.0 project. It’s a tiny enhancement, but good to know.

To enable Razor runtime compilation till ASP.NET Core 3.1

Till now, if you need to enable Razor runtime compilation, you will follow the below steps.

  • Install the Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation NuGet package.
  • Update the project’s Startup.ConfigureServices method to include a call to AddRazorRuntimeCompilation.
    public void ConfigureServices(IServiceCollection services)
    {
         services.AddControllersWithViews()
                 .AddRazorRuntimeCompilation();
    }
    
  • To enable Razor runtime compilation in ASP.NET Core 5.0

  • First, to use ASP.NET Core in .NET 5.0 Preview2 install the .NET 5.0 SDK.
  • Install the latest preview of Visual Studio 2019 16.6.
  • With ASP.NET Core 5, to enable razor file compilation, there is no need to explicitly add Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation NuGet package. When you create a new project targeted to ASP.NET Core 5, Visual Studio 2019 provides an option to enable this feature. Like,

    ASP.NET Core 5 support for Enabling Razor runtime compilation

    When you check this option, Visual Studio 2019 will add this package for you in dependencies.

    ASP.NET Core 5 support for Enabling Razor runtime compilation

    But, the following code is not present in the Startup.cs, which actually enables this feature via the code.

    services.AddControllersWithViews()
                 .AddRazorRuntimeCompilation();
    

    With ASP.NET core 5.0, this is handled via launchsetting.json file. You will see the following content in the launchsetting.json file.

    {
      "iisSettings": {
        "windowsAuthentication": false,
        "anonymousAuthentication": true,
        "iisExpress": {
          "applicationUrl": "http://localhost:55882",
          "sslPort": 44340
        }
      },
      "profiles": {
        "IIS Express": {
          "commandName": "IISExpress",
          "launchBrowser": true,
          "environmentVariables": {
            "ASPNETCORE_ENVIRONMENT": "Development",
            "ASPNETCORE_HOSTINGSTARTUPASSEMBLIES": "Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation"
          }
        },
        "ASPNETCore5App": {
          "commandName": "Project",
          "launchBrowser": true,
          "applicationUrl": "https://localhost:5001;http://localhost:5000",
          "environmentVariables": {
            "ASPNETCORE_ENVIRONMENT": "Development",
            "ASPNETCORE_HOSTINGSTARTUPASSEMBLIES": "Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation"
          }
        }
      }
    }
    

    To disable this on the development environment, comment or remove the line 16 and 25.

    While creating the project, if you forgot to check the “Enable Razor runtime compilation” option, then you can still enable it via adding the nuget package Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation and then configuring in the Startup.cs.

    Thank you for reading. Keep visiting this blog and share this in your network. Please put your thoughts and feedback in the comments section.

    PS: If you found this content valuable and want to return the favour, then Buy Me A Coffee

    3 thoughts to “ASP.NET Core 5 – Enabling Razor runtime compilation”

    1. I think this feature gets overlooked – it’s a great time saver in larger razor page projects as it basically allows you to skip the build step.
      If you combine it with an inline PageModel you can work on a complete razor page without building. Changes load instantly without “hot reload” which tends to be slow and glitchy.
      example IndexTest.cshtml:

      @page
      @using Microsoft.AspNetCore.Mvc.RazorPages

      @model IndexTestModel

      @functions{

      public class IndexTestModel : PageModel
      {
      public string TestProperty { get; set; }

      public void OnGet()
      {
      TestProperty = “Build not required!”;
      }
      }

      }

      @Model.TestProperty

    2. When I created a project with version ASP.NET 5.0 and use Web apllication (MVC) checked Enable Razor runtime compilation I got error when I try run IIS Express error looks like ”
      1>—— Build started: Project: WebApplication8, Configuration: Debug Any CPU ——
      1>You are using a preview version of .NET Core. See: https://aka.ms/dotnet-core-preview
      1>Unknown C# language version 9.0.
      1>C:\Program Files\dotnet\sdk\5.0.100-preview.2.20176.6\Sdks\Microsoft.NET.Sdk.Razor\build\netstandard2.0\Microsoft.NET.Sdk.Razor.CodeGeneration.targets(150,5): error : rzc generate exited with code 2.
      1>Done building project “WebApplication8.csproj” — FAILED.
      ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

      Maybe you can tell why?

    Leave a Reply

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