.NET Core MVC Page Not Refreshing After Changes

2020-02-07 18:44发布

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.

9条回答
SAY GOODBYE
2楼-- · 2020-02-07 19:05

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();
查看更多
来,给爷笑一个
3楼-- · 2020-02-07 19:06

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

查看更多
萌系小妹纸
4楼-- · 2020-02-07 19:07

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

查看更多
姐就是有狂的资本
5楼-- · 2020-02-07 19:09

In ASP.NET Core 3.0 (at the time of writing still in a preview!) RazorViewEngineOptions.AllowRecompilingViewsOnFileChangeis not available (or not accessible? - 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!):

  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

查看更多
爱情/是我丢掉的垃圾
6楼-- · 2020-02-07 19:10

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...
查看更多
对你真心纯属浪费
7楼-- · 2020-02-07 19:15

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));
    });
}
查看更多
登录 后发表回答