.NET Core MVC Page Not Refreshing After Changes

asp.net Core.Net Coreasp.net Core-Mvcasp.net Core-2.2

asp.net Core Problem Overview


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.

asp.net Core Solutions


Solution 1 - asp.net Core

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.

Solution 2 - asp.net Core

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.

Solution 3 - asp.net Core

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...

Solution 4 - asp.net Core

In addition to Alexander Christov's answer, since ASP.NET Core 3.1 you can enable the view compilation for development environment without changes to the Startup file:

  1. Install the Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation NuGet package.
  2. Set next environment variables (for example via environmentVariables section in launchSettings.json):
    • ASPNETCORE_ENVIRONMENT to "Development".
    • ASPNETCORE_HOSTINGSTARTUPASSEMBLIES to "Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation".

Solution 5 - asp.net Core

You should just add this:

services.AddControllersWithViews();

to the ConfigureService method.

Be aware below code is not available in ASP.NET Core 3.1:

services.AddControllersWithViews().AddRazorRuntimeCompilation();

Solution 6 - asp.net Core

For those using Net core 3.0 or greater

  1. Go to Tools → Nuget package manager → Manage nuget pakages for solution

  2. move to browse tab to browse from internet

  3. search for RuntimeCompilation Click on Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation

  4. install it on your intended projects the current stable version

  5. open Startup.cs file

  6. go to void method ConfigureServices

  7. add line: services.AddControllersWithViews().AddRazorRuntimeCompilation();

  8. you are DONE

Rerun and see. Now you can refresh your views or pages.

Solution 7 - asp.net Core

first of all install the Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation using nuget manager after that add below code into your startup.cs

services.AddRazorPages().AddRazorRuntimeCompilation();

Solution 8 - asp.net Core

Using .net core 2.2 running app with command dotnet watch run the project is restarted after every change

Solution 9 - asp.net Core

Below helped me when views were in separate project.

if(HostingEnvironment.IsDevelopment()){ // only in development (optional)
    services.AddMvc().AddRazorOptions(o => {
        o.FileProviders.Add(new PhysicalFileProvider(PATH_TO_PROJECT));
    });
}

Solution 10 - asp.net Core

I was able to solve this problem in Rider by adding the ASPNETCORE_ENVIRONMENT=Development environment variable.

Solution 11 - asp.net Core

There are two ways to resolve this issue:

1. Check the permissions of folder in which your .sln file present.There may be a problem with file access permissions as Visual studio may not be to access files when IIS express server is running, so to reflect new .cshtml changes each time you need to restart the server,so I suggest edit the folder access permissions by:

Right click on folder->properties->security->click on edit button -> check all options->save.

Restart Visual studio to see changes.

If this does not work then use 2 option.

2.In your project in startup.cs file add this below line ConfigureServices() in method :

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

Solution 12 - asp.net Core

Are you absolutely sure you are using 2.2? Check your csproj because it might be this bug https://github.com/aspnet/Razor/issues/2466 You could try turning off RazorCompileOnBuild more info https://docs.microsoft.com/en-us/aspnet/core/razor-pages/sdk?view=aspnetcore-2.1#properties

Solution 13 - asp.net Core

I had a similar problem upgrading from .net Core 3 to .net 5.0

Problem was due to old dependency in Telerik controls that we could not change.

Fixed by changing references in the .csproj file

<PackageReference Include="Microsoft.CodeAnalysis.Common" Version="3.8.0" />

to

<PackageReference Include="Microsoft.CodeAnalysis" Version="3.8.0" />

(your version may be different)

Solution 14 - asp.net Core

In Visual Studio 2022 Preview, it seems there is an option called Hot Reload for this purpose.

enter image description here

It seems to be available in Visual Studio 2019 too.

> With Hot Reload you can now modify your apps managed source code while > the application is running, without the need to manually pause or hit > a breakpoint. Simply make a supported change while your app is running > and in our new Visual Studio experience use the “apply code changes” > button to apply your edits.

https://devblogs.microsoft.com/dotnet/introducing-net-hot-reload/

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionkevskreeView Question on Stackoverflow
Solution 1 - asp.net CoreAlexander ChristovView Answer on Stackoverflow
Solution 2 - asp.net CoreChris AitchisonView Answer on Stackoverflow
Solution 3 - asp.net CoreJeremiah NView Answer on Stackoverflow
Solution 4 - asp.net CoreGuru StronView Answer on Stackoverflow
Solution 5 - asp.net CoreAlireza AbdollahnejadView Answer on Stackoverflow
Solution 6 - asp.net CoreGeraGamoView Answer on Stackoverflow
Solution 7 - asp.net CoreIshara SaminthaView Answer on Stackoverflow
Solution 8 - asp.net CorenanquimView Answer on Stackoverflow
Solution 9 - asp.net CoresmoqView Answer on Stackoverflow
Solution 10 - asp.net Coreuser1613512View Answer on Stackoverflow
Solution 11 - asp.net CoreOmkarView Answer on Stackoverflow
Solution 12 - asp.net CoreWantonView Answer on Stackoverflow
Solution 13 - asp.net CoreRuskinView Answer on Stackoverflow
Solution 14 - asp.net CorekarrView Answer on Stackoverflow