ChatGPT解决这个技术问题 Extra ChatGPT

.NET Core MVC Page Not Refreshing After Changes

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.

I'm encountering the same MissingMethodException you mentioned below... Did you ever figure it out? If, so could you please answer your question?
For use with Rider and/or Razor Class Libraries (RCL), see this answer.
Wow. Didn't see that coming. A real bummer. After 2 years this is upvoted only 147 times. Makes you wonder who's building apps with .NET Core...

A
Alexander Christov

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:

Add Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation NuGet package to the project Add the following in Startup.cs: services.AddControllersWithViews().AddRazorRuntimeCompilation();

Here's the full explanation for the curious.


This didn't work for me for whatever reason. I'm on Mac if that makes a difference.
This worked great for me on Mac and ASP.NET Core 3.1.
Thx for this hint; worked for me on .Net Core 3.1.2
Wowsers! 15 years working in .NET and it used to be easy. 1 day working with .NET Core and everything seems broken. And seriously - who decided it was a good idea to make HTML compiled and not changeable at runtime by default, then make developers hunt for a solution?
Thanks! Working on .Net 5.0!
M
Max

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.


Thank you. However, it appears that when I enable this, make a change in the html, and then refresh, I get the following exception: MissingMethodException: Method not found: 'Microsoft.Cci.IMethodReference Microsoft.Cci.ICustomAttribute.Constructor(Microsoft.CodeAnalysis.Emit.EmitContext)'. Microsoft.CodeAnalysis.CSharp.Symbol.Microsoft.CodeAnalysis.ISymbol.GetAttributes() Any idea on what this exception means? All of the other pages load fine. Only when I edit a file and refresh do I get this error
This worked for me. To set it depending on environment, add IHostingEnvironment to the Startup method and persist in a property. Then use something like services.AddMvc().AddRazorOptions(options => options.AllowRecompilingViewsOnFileChange = _env.IsEnvironment("MyEnvironment"));
thanks it worked. However I think it's pretty awkward and silly that Microsoft doesn't officially announce big changes like this.
@kevskree same thing is happening to me
@Mayank Gupta: see Alexander Christov's answer below (stackoverflow.com/a/57637903/198990). His answer worked for me (3.1).
J
Jeremiah N

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

This is the best answer without reading the article posted by Alex, which is worth the read.
G
Guru Stron

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:

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


This should be the accepted answer; it allows for the desired functionality without needing to alter the Startup file.
And if you upgrade a project from .NET 5 to .NET 6, you will need to update Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation from v5.x to v6.x as well or you won't be able to see changes in Razor Views or Pages as you make them.
A
Alireza Abdollahnejad

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();

You need to install nuget package to get it working Microsoft.AspNetCore.Mvc.Razor.Runtime
U
Uwe Keim

For those using Net core 3.0 or greater

Go to Tools → Nuget package manager → Manage nuget pakages for solution move to browse tab to browse from internet search for RuntimeCompilation Click on Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation install it on your intended projects the current stable version open Startup.cs file go to void method ConfigureServices add line: services.AddControllersWithViews().AddRazorRuntimeCompilation(); you are DONE

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


I
Ishara Samintha

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();


n
nanquim

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


s
smoq

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));
    });
}

This is the only answer that worked for me when working with Razor Class Libraries. Cheers!
Using Core 3.1 followed these directions, installed version 3.1.16 and received error: 'IMVcBuilder' does not contain a definition for 'AddRazorRuntimeCompilation' and no extension method 'AddRazorRuntimeCompilation' accepting a first argument of type 'IMVcBuilder' could be found.
u
user1613512

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


O
Omkar

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);


W
Wanton

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


R
Ruskin

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)


S
Saeed Adam

I had the same issue while working on a .NET 6 MVC Web App.

I installed Microsoft.AspNetCore.Mvc.Razor.Runtime.Compilation from NuGet Package Manger then added .AddRazorRuntimeCompilation(); after

builder.services.AddControllersWithViews();

so that it looks like this

builder.services.AddControllersWithViews().AddRazorRuntimeCompilation();

and it worked!

Hope this helped.


k
karr

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

https://i.stack.imgur.com/LZyD4.png

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/