Precompiling MVC Views in ASP.NET Core with .csproj’s

I didn’t see this documented on docs.asp.net yet for .csproj style projects, so I thought I’d blog about it, as it’s something I do on every project.

tldr;

Add this to your .csproj file:

2017-03-10_13-02-21.png

Alternatively, you can check out this GitHub commit that just adds pre-compiled views here.

What does precompiling views do?

Without precompiling your views, none of your views are compiled until they are requested for the first time.  This results in an extra few seconds of wait time for the end user.  Instead, most developers want to take a little bit of extra time in the publish step to precompile their views, so that users don’t have to eat that performance hit on the first request on every page.

Another benefit of precompiling your views is if there is a C# error on one of your .cshtml Razor pages (such as you changed the name of a property on your View Model and the rename didn’t update your .cshtml file), you will find out up front when you go to publish (via VS or a build server) rather than at run time when a user goes to request that page.

So now that we’ve seen what it does, let’s look at what it used to be in project.json.

What did it look like in project.json?

In project.json land, the pre-compiling of Views was a little verbose.  You had to do the following:

  1. Add a reference to “Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.Design” under the “dependencies” section
  2. Add a reference to “Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.Tools” under the tools section.
  3. Under buildOptions, set “preserveCompilationContext”to true
  4. And finally you had to add a fairly verbose post-publish script to actually use the pre-compile tool.All of that looked like this (removing the rest of the project.json for brevity):

This then generates a .PrecompiledViews.dll in the root of your publish directory.

How does it look in .csproj?

In project.json, it was pretty verbose.  So what does it look like in .csproj?  It’s much cleaner.

  1. Add  <PackageReference Include=Microsoft.AspNetCore.Mvc.Razor.ViewCompilation Version=1.1.0 />
  2. <MvcRazorCompileOnPublish>true</MvcRazorCompileOnPublish>
  3. Add <PreserveCompilationContext>true</PreserveCompilationContext>

So the final output of a .csproj might look a little something like this.  Note lines 5,6, and 32.

This will still generate a .PrecompiledViews.dll in the root of your publish directory.

This simplicity in order to get precompiled views is yet another win for the .csproj style ASP.NET Core projects over project.json.

One thought on “Precompiling MVC Views in ASP.NET Core with .csproj’s

  1. Thanks for this Scott, was just trying to remember what those elements were called.

    It’s worth noting that the Microsoft.AspNetCore.Mvc.Razor.ViewCompilation is a build-time only package, so you can avoid it appearing in your publish folder using PrivateAssets=”All”:

    Thanks again!

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s