Razor views are compiled at runtime when the view is invoked. ASP.NET Core 1.1.0 and higher can optionally compile Razor views and deploy them with the app — a process known as precompilation. The ASP.NET Core 2.x project templates enable precompilation by default.
Razor view precompilation is unavailable when doing a Self-Contained Deployment in ASP.NET Core versions 2.0.0 and earlier.
- Precompiling views results in a smaller published bundle and faster startup time.
- You can't edit Razor files after you precompile views. The edited views won't be present in the published bundle.
To deploy precompiled views:
If your project targets .NET Framework, include a package reference to
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.ViewCompilation" Version="2.0.0" PrivateAssets="All" />
If your project targets .NET Core, no changes are necessary.
The ASP.NET Core 2.x project templates implicitly set
true by default, which means this node can be safely removed from the .csproj file. If you prefer to be explicit, there's no harm in setting the
MvcRazorCompileOnPublish property to
true. The following .csproj sample highlights this setting:
<Project Sdk="Microsoft.NET.Sdk.Web"> <PropertyGroup> <TargetFramework>netcoreapp2.0</TargetFramework> <MvcRazorCompileOnPublish>true</MvcRazorCompileOnPublish> </PropertyGroup> <ItemGroup> <PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" /> </ItemGroup> </Project>