Loading... ## 问题 在`ASP.NET Core 3.0`中,对`Razor`页面修改刷新无法展示修改后的内容。 ## 原因 这是由于在`ASP.NET Core 3.0`中,对`Razor`视图和`Razor`页面的运行时编译的支持被移到了单独的程序包中,没有默认启用`Razor`运行时编译。 ## 解决方案 * 安装[Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation](https://www.nuget.org/packages/Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation) * 在`Startup`中启用`Razor`运行时编译: ```csharp public class Startup { 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 } } ``` # Reference * [\[Discussion\] Breaking changes to runtime compilation for Razor views and Razor Pages](https://github.com/dotnet/aspnetcore/issues/7647) * [Razor file compilation in ASP.NET Core](https://docs.microsoft.com/en-us/aspnet/core/mvc/views/view-compilation?view=aspnetcore-3.0&tabs=visual-studio) * [.cshtml change does not show on page refresh in ASP.NET Core 3](https://github.com/dotnet/aspnetcore/issues/8856) 最后修改:2020 年 09 月 22 日 © 允许规范转载 赞 0 如果觉得我的文章对你有用,请随意赞赏