Friday, June 22, 2018

Razor View compilation in ASP.NET Core

Update 26/6/2018: It can happen that what I describe below lead to much longer build times. This is a know issue. More information here: https://www.danielcrabtree.com/blog/444/speed-up-compilation-of-asp-net-core-2-1-projects

One of the ways to increase the speed of your ASP.NET MVC applications was by enabling view compilation. By default Razor views are not pre-compiled leading to longer startup times. Another disadvantage is that errors in views were not detected until you are running in the browser. Enabling view compilation solves this all.

Here is how to enable this in ASP.NET MVC: https://www.codeproject.com/Articles/1169354/Pre-compiled-Razor-View-in-ASP-NET-MVC

The question is, can you do the same thing in ASP.NET Core MVC?

In ASP.NET Core MVC, this behavior is enabled when the Microsoft.AspNetCore.Mvc.Razor.ViewCompilation package is referenced. As the Microsoft.AspNetCore.All package pulls in this package, this behavior is enabled by default for all new projects. It can be further controlled by setting the < MvcRazorCompileOnPublish> property. 

More information: https://docs.microsoft.com/en-us/aspnet/core/mvc/views/view-compilation?view=aspnetcore-2.1

No comments: