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. 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

51

.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>
Sign up to request clarification or add additional context in comments.

3 Comments

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?
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)
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.
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 Comments

I think there is no good way to do this now, you need to make some workaround here. See github.com/aspnet/MvcPrecompilation/issues/39.
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>

Comments

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()"

Comments

-3

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

4 Comments

I have migrated from 2.0 to 2.1. That said, that option is/was for publishing. My concern is debugging.
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?
This generaly disables it. How to enable it during publish then?
Take a look at ASP.NET Core Razor SDK for some options.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.