Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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> 
like image 273
Christian Gollhardt Avatar asked Jun 09 '18 21:06

Christian Gollhardt


People also ask

How do I enable razor runtime compilation?

Enable runtime compilation at project creationSelect either the Web Application or the Web Application (Model-View-Controller) project template. Select the Enable Razor runtime compilation checkbox.

How do I debug .NET core source code?

To debug . NET and ASP.NET Core source code in Visual Studio: In Tools -> Options -> Debugging -> General, un-check Enable Just My Code. Verify Enable Source Link support is checked.

Do Cshtml files get compiled?

Cshtml files are compiled by Razor into C# files. To track down some errors (or just to understand Razor) it might be useful to examine the C# code generated. For testing I created a small Razor view in an MVC4 project.

What is view component in ASP.NET Core?

A view component consists of two parts: the class (typically derived from ViewComponent) and the result it returns (typically a view). Like controllers, a view component can be a POCO, but most developers take advantage of the methods and properties available by deriving from ViewComponent .


2 Answers

.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> 
like image 197
Christian Gollhardt Avatar answered Sep 20 '22 22:09

Christian Gollhardt


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> 
like image 40
Nedzad G Avatar answered Sep 17 '22 22:09

Nedzad G