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

9

126

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?

Earthshaker answered 8/2, 2019 at 21:11 Comment(5)
.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".Poppycock
Doesn'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.Earthshaker
Are 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?Poppycock
PC, Visual Studio 2019 preview. I tried with 2.2 and it works! So there's something that changed in 3.0.Earthshaker
I am on a fully updated .net core 3/VS and had to add .AddRazorRuntimeCompilation(); Thank you!Isborne
V
69

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:
Vinny answered 25/2, 2019 at 23:51 Comment(2)
AddMvcRazorRuntimeCompilation is AddRazorRuntimeCompilation with ASP.Net Core 3.0 Preview 4Duo
I'm using .NET Core 6 and it worked for me in all of projects.Peraea
L
77

For ASP.NET Core 3 release version:

   services.AddControllersWithViews().AddRazorRuntimeCompilation();

https://learn.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
    }
Leucotomy answered 29/9, 2019 at 18:11 Comment(1)
Tip 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.Cogitable
V
69

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:
Vinny answered 25/2, 2019 at 23:51 Comment(2)
AddMvcRazorRuntimeCompilation is AddRazorRuntimeCompilation with ASP.Net Core 3.0 Preview 4Duo
I'm using .NET Core 6 and it worked for me in all of projects.Peraea
C
44

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
Cheapen answered 4/5, 2019 at 10:30 Comment(5)
Just a hint for others: when using Nuget, tick/check the "pre-release" box.Dejecta
Is 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.Norfolk
In 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/14572Monarch
+1 because removing the Microsoft.VisualStudio.Web.CodeGeneration.Design package was the secret sauce that nowhere else mentions.Hildagarde
@Hildagarde 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.Alyssaalyssum
P
28

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://learn.microsoft.com/en-us/aspnet/core/mvc/views/view-compilation?view=aspnetcore-3.0

Peculate answered 3/10, 2019 at 20:54 Comment(0)
H
20

To get runtime Razor Views Compilation back in ASP.NET Core 3.1:

  1. Install the Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation NuGet package.
  2. Update the project's Startup.ConfigureServices method to include a call to AddRazorRuntimeCompilation.
  3. services.AddRazorPages().AddRazorRuntimeCompilation();

    Razor file compilation in ASP.NET Core 3.1

Heterosexuality answered 13/1, 2020 at 10:57 Comment(0)
R
16

Step 1: Install the below package from NuGet Package Manager for Solution.

Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation

Step 2: Add the below code in ConfigureServices of Startup.cs file

services.AddControllersWithViews().AddRazorRuntimeCompilation();

Save the page and then refresh the client browser.

If you are using Razor then add the code for services.AddRazorPages().AddRazorRuntimeCompilation();

Republic answered 28/3, 2020 at 8:40 Comment(0)
K
13

In .NET Core 3.1, you need disable generation of the views.dll file.

  1. Add the below code to your .csproj file (in the root of your project):

    <PropertyGroup>
        <TargetFramework>netcoreapp3.1</TargetFramework>
        <UserSecretsId>...</UserSecretsId>
    
        <!-- add from this line: -->
        <CopyRazorGenerateFilesToPublishDirectory>true</CopyRazorGenerateFilesToPublishDirectory>
        <CopyRefAssembliesToPublishDirectory>false</CopyRefAssembliesToPublishDirectory>
        <MvcRazorCompileOnPublish>false</MvcRazorCompileOnPublish>
        <RazorCompileOnBuild>false</RazorCompileOnBuild>
        <!-- to this line. -->
    
    </PropertyGroup>
    
  2. Install the below package from the NuGet Package Manager for the Solution.

    Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation
    
  3. Add the below code to the ConfigureServices method of your Startup.cs file:

    services.AddMvc().AddRazorRuntimeCompilation();
    

    Or

    services.AddControllersWithViews().AddRazorRuntimeCompilation();
    

    Or

    services.AddRazorPages().AddRazorRuntimeCompilation();
    

After this, publish again and run it. It will work.

Khachaturian answered 28/6, 2020 at 11:52 Comment(2)
For 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 neededArmandinaarmando
Same as @Armandinaarmando for Razor Class Library with NetCore 6Valonia
H
7

Simpliest way to configure Runtime Compilation for Local Development only is to update launch profiles in launchSettings.json. This method requires no code changes to configure a project which is running locally (it will not affect your production).

See the official documentation:

  1. Install the Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation NuGet package.
  2. Modify the launch profile environmentVariables section in launchSettings.json:
    • Verify ASPNETCORE_ENVIRONMENT is set to "Development".
    • Set ASPNETCORE_HOSTINGSTARTUPASSEMBLIES to "Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation".

Example:

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:57676",
      "sslPort": 44364
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development",
        "ASPNETCORE_HOSTINGSTARTUPASSEMBLIES": "Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation"
      }
    },
    "RazorPagesApp": {
      "commandName": "Project",
      "launchBrowser": true,
      "applicationUrl": "https://localhost:5001;http://localhost:5000",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development",
        "ASPNETCORE_HOSTINGSTARTUPASSEMBLIES": "Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation"
      }
    }
  }
}
Higley answered 3/10, 2020 at 23:29 Comment(0)
B
6

Just an added note -- you may want to only conditionally enable this runtime compilation, so that the published output:

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

Conditionally enable runtime compilation

Bulletproof answered 15/1, 2020 at 16:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.