.NET Core MVC Page Not Refreshing After Changes

In ASP.NET Core 3.0 and higher, RazorViewEngineOptions.AllowRecompilingViewsOnFileChange is not available.

Surprised that refreshing a view while the app is running did not work, I discovered the following solution:

  1. Add Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation NuGet package to the project

  2. Add the following in Startup.cs:

    services.AddControllersWithViews().AddRazorRuntimeCompilation();
    

Here's the full explanation for the curious.


There was a change made in ASP.NET Core 2.2 it seems (and I can't find any announcements about this change). If you are not explicitly running in the 'Development' environment then the Razor Views are compiled and you will not see any changes made to the .cshtml

You can however turn off this using some config in your Startup class as follows.

services.AddMvc().AddRazorOptions(options => options.AllowRecompilingViewsOnFileChange = true);

For ASP.NET Core 3.0 and higher, see Alexander Christov's answer.


I've just created a new project using the latest ASP.NET MVC Core 3.1 template and I altered the following to get runtime recompilation enabled for Debug:

Reference NuGet package - Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation.

Startup.cs - ConfigureServices(IServiceCollection services) WAS:

// stuff...

services.AddControllersWithViews();

// more stuff...

NOW:

// stuff...

var mvcBuilder = services.AddControllersWithViews();

#if DEBUG
    mvcBuilder.AddRazorRuntimeCompilation();
#endif

// more stuff...