AspNet Core 3.0 and 3.1 : Enable runtime compilation for Razor Pages

Note as indicated in the "Razor file compilation in ASP.NET Core" posted by Givi, if you just want this feature for rapid development, you can enable this just for development and not for production by NOT adding

.AddRazorRuntimeCompilation()

to Startup.cs, and instead adding these environment variables to your launchSettings.json

"ASPNETCORE_HOSTINGSTARTUPASSEMBLIES": "Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation"
"ASPNETCORE_ENVIRONMENT" = "Development"

only for our integrated development environment (Dev appserver deployed through DevOps) and use

"ASPNETCORE_ENVIRONMENT" = "Local"

for individual developer's local development (in an accompanying appsettings.Local.json config file).

I'm happy to report that "ASPNETCORE_ENVIRONMENT" does not strictly need to be set to "Development" and that runtime compilation works for "Local" (and presumably whatever is the name you use for the environment you develop under).

But one thing I found was that although I added ASPNETCORE_HOSTINGSTARTUPASSEMBLIES to launchSettings.json, it was being removed (and thus not working) by the project properties Debug tab until I added the variable there (and now it works great).


Please use NuGet package Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation Version 3.1.7 that is compatible with ASP.Net Core 3.1 and apply following line of code in Startup.cs

 public void ConfigureServices(IServiceCollection services)
    {
      services.AddControllersWithViews();            
      services.AddControllersWithViews().AddRazorRuntimeCompilation();    
    }

For this issue, I suggest you try to install package Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation and then configure AddRazorRuntimeCompilation in Startup.cs like

public void ConfigureServices(IServiceCollection services)
{
    services.AddControllersWithViews().AddRazorRuntimeCompilation();
}

For this issue, you could trace by Breaking changes to runtime compilation for Razor views and Razor Pages #343