问题
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.cs
and Program.cs
with no resolution.
回答1:
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);
回答2:
In ASP.NET Core 3.0 (at the time of writing still in a preview!) RazorViewEngineOptions.AllowRecompilingViewsOnFileChange
is not available (or not accessibe? - had no time to check).
Surprised that refreshing a view while the app is running did not work I discovered the following solution (beware, things might change in the release!):
- 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...
HTH
回答3:
If you are using .Net Core 3.0, use RazorRuntimeCompilationMvcBuilderExtensions extension method AddRazorRuntimeCompilation()
Steps to follow:
- Install the NuGet package
Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation
which is available here. Add the following code in
ConfigureServices
method ofStartup.cs
services.AddControllersWithViews().AddRazorRuntimeCompilation();
回答4:
Using .net core 2.2 running app with command dotnet watch run the project is restarted after every change
回答5:
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));
});
}
回答6:
I was able to solve this problem in Rider by adding the ASPNETCORE_ENVIRONMENT=Development
environment variable.
回答7:
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
回答8:
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);
来源:https://stackoverflow.com/questions/53639969/net-core-mvc-page-not-refreshing-after-changes