"
ASP.NET (snapshot 2017) Microsoft documentation and samples

Razor view compilation and precompilation in ASP.NET Core

By Rick Anderson

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.

[!NOTE] Razor view precompilation is currently unavailable when performing a self-contained deployment (SCD) in ASP.NET Core 2.0. The feature will be available for SCDs when 2.1 releases. For more information, see View compilation fails when cross-compiling for Linux on Windows.

Precompilation considerations:

To deploy precompiled views:

ASP.NET Core 2.x

If your project targets .NET Framework, include a package reference to Microsoft.AspNetCore.Mvc.Razor.ViewCompilation:

If your project targets .NET Core, no changes are necessary.

The ASP.NET Core 2.x project templates implicitly set MvcRazorCompileOnPublish to 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:

[!code-xmlMain]

   1:  <Project Sdk="Microsoft.NET.Sdk.Web">
   2:   
   3:    <PropertyGroup>
   4:      <TargetFramework>netcoreapp2.0</TargetFramework>
   5:      <MvcRazorCompileOnPublish>true</MvcRazorCompileOnPublish>
   6:    </PropertyGroup>
   7:   
   8:    <ItemGroup>
   9:      <PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" />
  10:    </ItemGroup>
  11:   
  12:  </Project>

ASP.NET Core 1.x

Set MvcRazorCompileOnPublish to true, and include a package reference to Microsoft.AspNetCore.Mvc.Razor.ViewCompilation. The following .csproj sample highlights these settings:

[!code-xmlMain]

   1:  <Project Sdk="Microsoft.NET.Sdk.Web">
   2:   
   3:    <PropertyGroup>
   4:      <TargetFramework>netcoreapp1.1</TargetFramework>
   5:      <MvcRazorCompileOnPublish>true</MvcRazorCompileOnPublish>
   6:    </PropertyGroup>
   7:   
   8:    <ItemGroup>
   9:      <PackageReference Include="Microsoft.AspNetCore" Version="1.1.0" />
  10:      <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.0" />
  11:      <PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="1.1.0" />
  12:      <PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.ViewCompilation" Version="1.1.0-*" />
  13:    </ItemGroup>
  14:   
  15:  </Project>


A .PrecompiledViews.dll file, containing the compiled Razor views, is produced when precompilation succeeds. For example, the screenshot below depicts the contents of Index.cshtml inside of WebApplication1.PrecompiledViews.dll:

Razor views inside DLL
Razor views inside DLL


Comments ( )
Link to this page: //www.vb-net.com/AspNet-DocAndSamples-2017/aspnetcore/mvc/views/view-compilation.htm
< THANKS ME>