.NET Core MVC Page Not Refreshing After Changes

kevskree picture kevskree · Dec 5, 2018 · Viewed 37.9k times · Source

I'm building a .NET Core MVC on the latest version 2.2. I have a problem when I make changes to the CSHTML file and refresh the page, my changes are not reflected in the browser. I have to restart the project in order to see my changes. This has been happening for a while now so I'm not exactly sure what change caused this issue.

I've tried using the chrome's "Empty Cache and Hard Reload" as well as other browsers to no avail. This happens on Windows and Mac using both Visual Studio for Mac and VS Code

In a default .Net Core project it works fine so it must be something in my project that changed along the way. I'm wondering where I need to start in order to debug this issue? I've tried commenting out almost everything in my Startup.csand Program.cs with no resolution.

Answer

Alexander Christov picture Alexander Christov · Aug 24, 2019

In ASP.NET Core 3.0 and higher, RazorViewEngineOptions.AllowRecompilingViewsOnFileChangeis 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...

HTH