.NET Core MVC Page Not Refreshing After Changes

后端 未结 10 447
花落未央
花落未央 2020-12-04 10:09

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 br

相关标签:
10条回答
  • 2020-12-04 10:22

    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.

    0 讨论(0)
  • 2020-12-04 10:23

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

    0 讨论(0)
  • 2020-12-04 10:28

    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

    0 讨论(0)
  • 2020-12-04 10:32

    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.

    0 讨论(0)
  • 2020-12-04 10:32

    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();
    
    0 讨论(0)
  • 2020-12-04 10:32

    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));
        });
    }
    
    0 讨论(0)
提交回复
热议问题