25

Yesterday I updated to net core 2.1.

Now if I am debugging, the views getting precompiled, which ofcourse takes a long time during startup... Is it possible to fall back to the previous behavior, where the Views are compiled just in time, if it is needed?

Output

I have no reference related to precompilation in my csproj. Is it something that comes from the meta package?

  <ItemGroup>
    <PackageReference Include="JetBrains.Annotations" Version="11.1.0" />
    <PackageReference Include="Microsoft.AspNetCore.All" Version="2.1.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.1.0" PrivateAssets="All" />
    <PackageReference Include="Swashbuckle.AspNetCore" Version="2.5.0" />
    <!--<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.ViewCompilation" Version="2.0.0" PrivateAssets="All" />-->
  </ItemGroup>
Christian Gollhardt
  • 16,510
  • 17
  • 74
  • 111
  • Hi there, I was wondering if you know where the compiled views are stored? Thanks in advance. – IceCode Nov 02 '18 at 08:19
  • 1
    They are just normal classes included in the `Views.dll`. You can show what's in the dll with tools like [dotPeek](https://www.jetbrains.com/decompiler/) @Örvar – Christian Gollhardt Nov 02 '18 at 08:33

5 Answers5

50

.net core >= 3 (also called .net 5)

Microsoft created a Nuget Package. This is documented here.

Just reference Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation in your .csproj file conditionally. Don't forget to adjust the version, you actualy use.

<PackageReference
    Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation"
    Version="3.1.0"
    Condition="'$(Configuration)' == 'Debug'" />

Also configure your services

    public void ConfigureServices(IServiceCollection services)
    {
        // your MVC Builder (AddRazorPages/AddControllersWithViews)
        IMvcBuilder builder = services.AddRazorPages();

#if DEBUG
            // Only use Runtime Compilation on Debug
            if (Env.IsDevelopment())
            {
                builder.AddRazorRuntimeCompilation();
            }
#endif
    }

Ofcourse, when you want to general use Runtime Compilation, even when published, you don't need all the conditions.

.net core >= 2.1 && < 3

This can be accomplished using the property RazorCompileOnBuild in the .csproj file:

<PropertyGroup>
  <TargetFramework>netcoreapp2.1</TargetFramework>
  <RazorCompileOnBuild>false</RazorCompileOnBuild>
  <RazorCompileOnPublish>true</RazorCompileOnPublish>
</PropertyGroup>

This way the Razor files are only precompiled during publishing.

Depending on the usecase you also want to configure this depending on the build configuration:

<PropertyGroup Condition="'$(Configuration)' == 'Debug'">
  <RazorCompileOnBuild>false</RazorCompileOnBuild>
  <RazorCompileOnPublish>true</RazorCompileOnPublish>
</PropertyGroup>
Christian Gollhardt
  • 16,510
  • 17
  • 74
  • 111
  • Using this setting is pretty cool in debug mode in that the time to first render remains roughly the same and F5 in the browser to refresh after an cshtml change is MUCH faster. But, with this config approach, F1 in VS 2017 no longer causes my linked browsers to refresh, instead VS puts up a dialog that says "Do you want to stop debugging. Kinda odd. Any thoughts? – RonC Jul 27 '18 at 18:17
  • 1
    For .Net Core 3.x you need to install a NuGet Extension from MS https://learn.microsoft.com/en-us/aspnet/core/mvc/views/view-compilation?view=aspnetcore-3.0 - Note the "ENV" property needs to be added to the Startup constructor as a DI param (as per comment at the bottom of page) – Rob Feb 25 '20 at 10:55
1

You should set MvcRazorCompileOnPublish to false, with this, it will turn off all functions of view compilation that are enabled as part of publishing.

<PropertyGroup>
  <MvcRazorCompileOnPublish>false</MvcRazorCompileOnPublish>
</PropertyGroup>
Nedzad G
  • 1,007
  • 1
  • 10
  • 21
  • [See this](https://stackoverflow.com/questions/50778521/how-to-disable-precompiled-views-in-net-core-2-1-for-debugging/50778601#comment88564004_50778565) – Christian Gollhardt Jun 09 '18 at 21:15
  • 2
    I think there is no good way to do this now, you need to make some workaround here. See https://github.com/aspnet/MvcPrecompilation/issues/39. – Nedzad G Jun 09 '18 at 21:43
0

You may play with MvcRazorFilesToCompile project property

MvcRazorFilesToCompile: item group that specifies view files to compile. By default this includes all .cshtml files marked as content.

Note: don't use the empty string, as this is the same as default (from repo):

<ItemGroup Condition="'@(MvcRazorFilesToCompile)' == ''">
      <MvcRazorFilesToCompile Include="@(Content)" Condition="'%(Extension)'=='.cshtml'" />
</ItemGroup>
Set
  • 47,577
  • 22
  • 132
  • 150
0

If you using ControllersWithViews on .net core 3.1 or .net core 5.0, you can look at following solution:

Disable precompiled views on development when using "AddControllersWithViews()"

serhattsnmz
  • 167
  • 1
  • 11
-3

From Migrate from ASP.NET Core 1.x to 2.0 guide you will need to set MvcRazorCompileOnPublish to false.

Mark G
  • 2,848
  • 1
  • 24
  • 32
  • I have migrated from 2.0 to 2.1. That said, that option is/was for publishing. My concern is debugging. – Christian Gollhardt Jun 09 '18 at 21:12
  • It seems you have dismissed the answer without even trying it. If you delete Conversiolt.Conmania.Views.dll and add this flag does it get recreated in the debug folder? – Mark G Jun 09 '18 at 21:38
  • This generaly disables it. How to enable it during publish then? – Christian Gollhardt Jun 09 '18 at 21:43
  • 1
    Take a look at [ASP.NET Core Razor SDK](https://learn.microsoft.com/en-us/aspnet/core/mvc/razor-pages/sdk#using-the-razor-sdk) for some options. – Mark G Jun 09 '18 at 22:04