.NET Core 3.0: Razor views don't automatically recompile on change
Solution 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 toAddMvcRazorRuntimeCompilation
:
Solution 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
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
}
Solution 3
To get runtime view compilation back in ASP.NET Core
3
- Reference
Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation
- Call
services.AddMvc().AddRazorRuntimeCompilation()
- Remove
Microsoft.VisualStudio.Web.CodeGeneration.Design
if there's a version mismatch on theMicrosoft.CodeAnalysis.Common
package
Solution 4
Runtime compilation is enabled using the Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation
package. To enable runtime compilation, apps must:
Install the
Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation
NuGet package.Update the project's
Startup.ConfigureServices
method to include a call toAddRazorRuntimeCompilation
:
services
.AddControllersWithViews()
.AddRazorRuntimeCompilation();
https://docs.microsoft.com/en-us/aspnet/core/mvc/views/view-compilation?view=aspnetcore-3.0
Solution 5
To get runtime Razor Views Compilation back in ASP.NET Core 3.1:
- Install the
Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation
NuGet package. - Update the project's
Startup.ConfigureServices
method to include a call toAddRazorRuntimeCompilation
. -
services.AddRazorPages().AddRazorRuntimeCompilation()
;
Related videos on Youtube
tarun713
Developer, architect, product manager, and so much more.
Updated on July 08, 2022Comments
-
tarun713 almost 2 years
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 inStartup.cs
after adding theMicrosoft.AspNetCore.Mvc.Razor.RuntimeCompilation
NuGet package. In my case, I am only using Razor Pages, so I don't callAddControllersWithViews()
. Instead, this worked for me:services.AddRazorPages().AddRazorRuntimeCompilation();
-
John-Luke Laue about 5 years.cshtml razor views do get recompiled. Can you double check? Try adding a <h1>Test</h1>. Start the app, load the page. you should see "Test". Then change this to <h1>Foo</h1>. You should see "Foo".
-
tarun713 about 5 yearsDoesn't work! I have to stop and start the solution. Captured it here: drive.google.com/file/d/1xOWQK2SvE2dskSYRdLz9X7iEmAv7BcTN/… - Have tried on multiple machines with the stock Razor Pages template.
-
John-Luke Laue about 5 yearsAre you running a on mac or pc or linux? Also, it might be an issue with the preview. Can you try using asp.net core 2.x?
-
tarun713 about 5 yearsPC, Visual Studio 2019 preview. I tried with 2.2 and it works! So there's something that changed in 3.0.
-
duck over 4 yearsI am on a fully updated .net core 3/VS and had to add
.AddRazorRuntimeCompilation();
Thank you!
-
-
D.B. Fred about 5 years
AddMvcRazorRuntimeCompilation
isAddRazorRuntimeCompilation
with ASP.Net Core 3.0 Preview 4 -
GeoffM almost 5 yearsJust a hint for others: when using Nuget, tick/check the "pre-release" box.
-
Chet over 4 yearsIs there a way to prevent the assets from being deployed to production? I tried some combinations of PrivateAssets on the PackageReference but it doesn't seem to work.
-
dariol over 4 yearsIn Core 2.2 razor options has FileProviders and it works even without any recompilation. Just edit cshtml and voila. In core 3 this property is removed. :( github.com/aspnet/AspNetCore/issues/14572
-
tkburbidge about 4 years+1 because removing the Microsoft.VisualStudio.Web.CodeGeneration.Design package was the secret sauce that nowhere else mentions.
-
Lynn Crumbling about 4 years@tkburbidge I just ran into that issue with the package, too. Oddly enough, I had two lines that were exactly the same, back-to-back. I removed both, and could compile. Even more oddly, I then put the lines back in place, and was still able to compile.
-
Tore Aurstad almost 4 yearsTip about versions of the nuget package related to .Net core version. In case you use .Net Core 3.0, you cannot select the later versions of this Nuget package. But version 3.0.0 of the Nuget lib works with .Net Core 3.0. If you use .Net Core 3.1, you can select newer versions.
-
Nathan over 2 yearsFor my .NET 5.0 only the following tags were needed inside .csproj
<RazorCompileOnBuild>false</RazorCompileOnBuild> <RazorCompileOnPublish>false</RazorCompileOnPublish> <CopyRefAssembliesToPublishDirectory>false</CopyRefAssembliesToPublishDirectory>
All other 2 & 3 steps also needed -
Marcelo Sader almost 2 yearsI'm using .NET Core 6 and it worked for me in all of projects.