Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
524 views
in Technique[技术] by (71.8m points)

c# - .NET Core 3.0: Razor views don't automatically recompile on change

According to the documentation, Razor views should, by default, recompile on change on local environments for ASP.NET Core 3.0.

However, my project doesn't do this locally. If I change a view and refresh when I'm debugging locally, the change is not reflected. I have to stop the solution, re-run, and then see the change.

I am doing this on a default ASP.NET Core Web Application template on Visual Studio 2019 with ASP.NET Core 3.0.0 Preview 2 using Razor pages. Any idea if I need to change settings to enable this feature?

UPDATE NOV 2019 FOR 3.0 FULL RELEASE:

This question still gets a lot of views. A few answers have cited to add

services.AddControllersWithViews().AddRazorRuntimeCompilation(); 

To your ConfigureServices() function in Startup.cs after adding the Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation NuGet package. In my case, I am only using Razor Pages, so I don't call AddControllersWithViews(). Instead, this worked for me:

services.AddRazorPages().AddRazorRuntimeCompilation();
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

For ASP.NET Core 3 release version:

   services.AddControllersWithViews().AddRazorRuntimeCompilation();

https://docs.microsoft.com/en-us/aspnet/core/mvc/views/view-compilation?view=aspnetcore-3.0

It can also be enabled conditionally only for local development, quoted from the link:

Runtime compilation can be enabled such that it's only available for local development. Conditionally enabling in this manner ensures that the published output:

Uses compiled views.
Is smaller in size.
Doesn't enable file watchers in production.

   public Startup(IConfiguration configuration, IWebHostEnvironment env)
    {
        Configuration = configuration;
        Env = env;
    }

    public IWebHostEnvironment Env { get; set; }
    public IConfiguration Configuration { get; }

    public void ConfigureServices(IServiceCollection services)
    {
        IMvcBuilder builder = services.AddRazorPages();

#if DEBUG
            if (Env.IsDevelopment())
            {
                builder.AddRazorRuntimeCompilation();
            }
#endif
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...