.Net Core 3.0: Razor Views Don't Automatically Recompile on Change

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

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:

Razor Page Runtime Compilation not working

It looks like you need the RazorCompileOnPublish property, which you can set in your .csproj file:

<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<RazorCompileOnPublish>false</RazorCompileOnPublish>
...
</PropertyGroup>

...
</Project>

Setting RazorCompileOnPublish to false should be all that's needed, but I suspect you don't need that CopyRefAssembliesToPublishDirectory property in your .csproj.

AspNet Core 3.0 and 3.1 : Enable runtime compilation for Razor Pages

For this issue, I suggest you try to install package Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation and then configure AddRazorRuntimeCompilation in Startup.cs like

public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews().AddRazorRuntimeCompilation();
}

For this issue, you could trace by Breaking changes to runtime compilation for Razor views and Razor Pages #343

.NET Core MVC Page Not Refreshing After Changes

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.

Asp net core 3.1 webapp doesn't refresh on modify razor template

To enable runtime compilation in vs code, you need to follow below steps:

1.Add a reference to the Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation package,run the command in the terminal

dotnet add package Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation -v 3.1.0

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

services.AddControllersWithViews().AddRazorRuntimeCompilation();//for MVC

//services.AddRazorPages().AddRazorRuntimeCompilation();//for Razor Pages

Refer to https://learn.microsoft.com/en-us/aspnet/core/mvc/views/view-compilation?view=aspnetcore-3.1#runtime-compilation

VS .Net Core Razor pages do not update unless project is re-started

Microsoft mentioned in the documentation to add runtime compilation.

Runtime compilation may be optionally enabled by configuring your application.

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

Add Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation package.

Update Startup.cs

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

How to fix NuGet package Razor runtime compilation Error?

Add <RazorCompileOnBuild>false</RazorCompileOnBuild> to your .csproj file. That should allow you to build the project.

You also might need <MvcRazorCompileOnPublish>false</MvcRazorCompileOnPublish> for publishing to a server.

See example below:

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
...
<RazorCompileOnBuild>false</RazorCompileOnBuild>
<MvcRazorCompileOnPublish>false</MvcRazorCompileOnPublish>
...
</PropertyGroup>

Found this property from this answer on a related question.

will it auto compile when a razor page file is updated on server?

That depends on your development environment. Assuming you are using Visual Studio as your IDE, usually yes, you will be both able to update your views and code-behind files during a debugging session, if you are running a debug session, and using a debug build (personally, I have never tried to do this in a non-debug build, as I actually see no point in doing so).

I say "usually" because there are some situations which the IDE is not able to update the code which runs behind your app's deployment. Most of the times, if you just alter the code of method bodies in your code-behind files or change some things at your view, things will work smoothly (although you might notice some delay at the first time you reload a page that has been affected by your changes: this is because the IDE is updating your deployment behind the scenes when you reload that page).

However, if you try to do some more complicated things, like editting the signature of methods, declaring new properties in one of your classes, or injecting new stuff in your views, the development environment might have some trouble to update your deploy. Check the screenshot below for a message given by Visual Studio Community 2017 when I try to add a new parameter to a method of a controller I have in one of my projects: the IDE warns me that it won't be able to upload my deployment in the server, and thus my changes won't be correctly reflected during my current debug.

VS 2017 warning

Also, one thing to keep in mind is that Visual Studio mostly will only allow you to make editions to your code when you pause the debug session. Code will be in read-only state if the application is currently running.



Related Topics



Leave a reply



Submit