In < MVC6 I could have .cshtml files pre-compiled on publish, so that they didn't have to be compiled on first hit when requested.
Is it possible to precompile .cshtml files in MVC6?
Build-time and publish-time compilation of Razor files is enabled by default by the Razor SDK. When enabled, runtime compilation complements build-time compilation, allowing Razor files to be updated if they're edited.
MvcBuildViews builds views temporarily and gives build results back to building process. If there were errors then build fails and errors are shown in Visual Studio errors window.
Add a reference to Microsoft.AspNetCore.Mvc.Razor.ViewCompilation
:
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.ViewCompilation" Version="1.1.0-msbuild3-update1" PrivateAssets="All" />
</ItemGroup>
Turn on razor view compilation upon publishing the project.
<PropertyGroup>
<MvcRazorCompileOnPublish>true</MvcRazorCompileOnPublish>
</PropertyGroup>
See this GitHub issue for more details.
Razor Pre-compilation was dropped in ASP.NET Core 1.0 MVC 6 RC2 but was brought back in ASP.NET Core 1.1 using a tool which you can add like so:
Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.Design
under the dependencies
section like so:"Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.Design": { "type": "build", "version": "1.1.0-preview4-final" }
Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.Tools
under the tools
section like so:"Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.Tools": "1.1.0-preview4-final",
"scripts": { "postpublish": "dotnet razor-precompile --configuration %publish:Configuration% --framework %publish:TargetFramework% --output-path %publish:OutputPath% %publish:ProjectPath%" }
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