Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
395 views
in Technique[技术] by (71.8m points)

c# - How to disable precompiled views in net core 2.1+ / net 5 for debugging?

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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...