27

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>
2
  • Hi there, I was wondering if you know where the compiled views are stored? Thanks in advance.
    – IceCode
    Commented Nov 2, 2018 at 8:19
  • 1
    They are just normal classes included in the Views.dll. You can show what's in the dll with tools like dotPeek @Örvar Commented Nov 2, 2018 at 8:33

5 Answers 5

52

.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>
3
  • 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
    Commented Jul 27, 2018 at 18:17
  • 2
    For .Net Core 3.x you need to install a NuGet Extension from MS learn.microsoft.com/en-us/aspnet/core/mvc/views/… - 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
    Commented Feb 25, 2020 at 10:55
  • We just updated our .net framework API to 64 bit and started getting an error where it was failing when trying to load xyz.Views.dll file complaining that it's not a valid Win32 application. Our application doesn't even have any Views but because it references Microsoft.AspNetCore.Mvc package, it produces that file. Adding the above RazorCompile... settings solved the isue and that DLL is not produced in the BIN folder anymore. Commented Sep 4, 2023 at 2:02
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>
2
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>
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()"

-3

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

4
  • I have migrated from 2.0 to 2.1. That said, that option is/was for publishing. My concern is debugging. Commented Jun 9, 2018 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
    Commented Jun 9, 2018 at 21:38
  • This generaly disables it. How to enable it during publish then? Commented Jun 9, 2018 at 21:43
  • 1
    Take a look at ASP.NET Core Razor SDK for some options.
    – Mark G
    Commented Jun 9, 2018 at 22:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.