.NET Core 3.0 - Preview 2 - Razor views don't automatically recompile on change

大憨熊 提交于 2019-12-17 22:26:18

问题


According to https://docs.microsoft.com/en-us/aspnet/core/mvc/views/view-compilation?view=aspnetcore-3.0 by default razor views should 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 does not reflect. 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, so this worked for me:

services.AddRazorPages().AddRazorRuntimeCompilation();

回答1:


OK it looks like it's not supported yet :(

Runtime compilation removed As a consequence of cleaning up the ASP.NET Core shared framework to not depend on Roslyn, support for runtime compilation of pages and views has also been removed in this preview release. Instead compilation of pages and views is performed at build time. In a future preview update we will provide a NuGet packages for optionally enabling runtime compilation support in an app.

You can read more about the issue here https://github.com/aspnet/Announcements/issues/343

Applications that require runtime compilation or re-compilation of Razor files should:

  • Add a reference to the Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation package. It'll be available as part of the 3.0.0-preview3 release.
  • Update the application's ConfigureServices to include a call to AddMvcRazorRuntimeCompilation:



回答2:


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




回答3:


To get runtime view compilation back in ASP.NET Core 3

  1. Reference Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation
  2. Call services.AddMvc().AddRazorRuntimeCompilation()
  3. Remove Microsoft.VisualStudio.Web.CodeGeneration.Design if there's a version mismatch on the Microsoft.CodeAnalysis.Common package



回答4:


Runtime compilation is enabled using the Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation package. To enable runtime compilation, apps must:

  1. Install the Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation NuGet package.

  2. Update the project's Startup.ConfigureServices method to include a call to AddRazorRuntimeCompilation:

services
    .AddControllersWithViews()
    .AddRazorRuntimeCompilation();

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



来源:https://stackoverflow.com/questions/54600273/net-core-3-0-preview-2-razor-views-dont-automatically-recompile-on-change

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!