How to disable precompiled views in net core 2.1 for debugging?

旧城冷巷雨未停 提交于 2019-12-21 03:34:10

问题


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?

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>

回答1:


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)|$(Platform)'=='Debug|AnyCPU'">
  <RazorCompileOnBuild>false</RazorCompileOnBuild>
  <RazorCompileOnPublish>true</RazorCompileOnPublish>
</PropertyGroup>

Many thanks to Mark G pointing me into the correct direction.




回答2:


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>



回答3:


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>



回答4:


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



来源:https://stackoverflow.com/questions/50778521/how-to-disable-precompiled-views-in-net-core-2-1-for-debugging

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!