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>
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.
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.
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.
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 .
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.
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>
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With