ASP.NET MVC: Precompiling views

This blog post is about how to really precompile ASP.NET MVC views. It destroys the myth about MvcBuildViews as precompilation tool and shows you how to configure precompiling. It also stops on some internals of precompiling and explains what are the options and limitations with it. There is also real-life example for Azure where precompiling of views may avoid some serious headaches that one may face otherwise.

What is MvcBuildViews?

MvcBuildViews is often mistakenly considered as something that when activated results in precompiled views. It doesn’t, actually. It is just a thing to include views to build process but it doesn’t compile these to project binaries folder. 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.

View building error in Visual Studio

You can find out more about MvcBuildViews from Jim Lamb blog post Turn on Compile-time View Checking for ASP.NET MVC Projects in TFS Build 2010.

How to precompile views?

As precompiling is kind of expensive and time consuming process you really want to do it when publishing application to test or live environment. Simplest way to do it is using web site publishing. Publishing configuration has settings page where you can turn on compiling of views.

ASP.NET publishing settings

If you publish your web application then you will discover that only global.asax has something precompiled for it. But there is nothing for views.

By default it is expected that there can be updates to views and it is possible only if views are not compiled to assembly. As soon as there is assembly for views modifications to view files are not automatically reflected in application output.

Let’s open advanced settings and turn off updates to views. As ASP.NET compiler creates separate assembly for every view it’s possible we get big number of very small assemblies if there are many views. It’s possible to specify the name of assembly where all those small view assemblies are merged.

ASP.NET precompile settings

Now you are good to go with precompiled views when publishing your application.

NB! Although it may seem like good idea to have precompiled views available also on dev boxes, I suggest you to abandon this idea. Precompiling takes time more than developers want to wait and it slows down their progress as after every build they must wait until precompiling is done.

NB! If you precompile views then views are not updatable anymore. It means that if you change view file on server you don’t see any changes in output. This is because precompiled views are called from DLL-file. You have to publish your application again if you made changes to views.

Precompiled views after publishing

After publishing precompiled views can be found from bin folder of web application. In my case there is AppCode.dll file that contains code for all precompiled views. But besides this there are also files with .compiled extension. These files tell ASP.NET where is output of given view.

In my case there’s file _smsdialog.cshtml.49841770.compiled in bin folder. Here is the content of this file.

<?xml version=1.0 encoding=utf-8?>
<preserve resultType=2 virtualPath=/Views/Dialogs/_SmsDialog.cshtml 
          hash=ffffffffda035ab6 filehash=166a10c1bc59 flags=110000 
          assembly=AppCode type=ASP._Page_Views_Dialogs__SmsDialog_cshtml>
    <filedep name=/Views/Dialogs/_SmsDialog.cshtml />

It defines file dependency and specifies where is the code for this view (assembly and type attributes of preserve node).

If I open AppCode.dll with decompiler I can see the source code of precompiled view.

Source code of precompiled view

As precompiling is done by ASP.NET command-line utilities you can also use precompiling on your build server.

Precompiled views on Azure

I worked on application that has some complex views. Compiling this views on runtime is slow even on dev box wit fast hard disc. It turned out to be times slower on Azure compute instance where web application is hosted. I investigated virtual machine that runs compute role and made the following discoveries:

  • regular discs on Azure are very slow,
  • compared to similar discs available they beat them only with one thing: the energy consumption,
  • SSD-s are way more expensive on Azure that regular discs,
  • the less disc I/O application generated the faster it is.

This is ideal case for precompiling the views. It’s not possible to avoid those .compiled files but getting all code for views to one assembly makes application startup faster because there’s only one library to load. With precompiled views your application works way faster after deployment because ASP.NET doesn’t have to build and compile views to compute role hard disc.

Wrapping up

The main take-away of this post is: MvcBuildViews is here to make building of views as part of web application building process. It doesn’t generate any output to binaries folder. Views are precompiled during publishing process and precompiling happens only if views are not expected to be modified. It is possible to configure how precompilation is done. This way it is possible to merge binaries of precompiled views to one assembly. This way it is possible to shorten the time it takes for application to start. As it turns out then precompiled views are good choice for web applications that run on Azure as it avoids some really slow disc I/O and web application gets highly responsive way faster than otherwise.

Liked this post? Empower your friends by sharing it!

Gunnar Peipman

Gunnar Peipman is ASP.NET, Azure and SharePoint fan, Estonian Microsoft user group leader, blogger, conference speaker, teacher, and tech maniac. Since 2008 he is Microsoft MVP specialized on ASP.NET.

    15 thoughts on “ASP.NET MVC: Precompiling views

    • August 16, 2016 at 11:31 am

      Informative. Dispelled a lot of myths about pre-compilation. Do yiu have any tips for improving the peofdmance of the aspnet_compiler during publishing. In my project this step takes almost 1 hour and it’s not uncommon from what I understand.

    • August 16, 2016 at 12:08 pm

      I suggest you to check you dev box disc performance. Visual Studio and ASP.NET web tools may cause massive disc I/O when building, compiling or generating things. If disc is the bottle neck then switch over to SSD.

    • August 16, 2016 at 1:43 pm

      Have you every encounter a failure on Azure with precompiled views?
      It worked on the dev machine but when deployed on the azure server it would not run.
      I had but could not solve it and went back to deployment without precompiled views …

      // Ryan

    • August 16, 2016 at 1:53 pm

      > As precompiling is done by ASP.NET command-line utilities you can also use precompiling on your build server.

      Do you have examples of those command line utilities?

      When I try your steps I get some failures because the post-compiler does not recognize C# 6 features (specifically string interpolation).

      Thanks for the post, this is something I’ve been wanting to nail down for a long time!

    • August 16, 2016 at 2:28 pm

      *Update* on the C# 6 syntax not recognized error: I changed my web.config to “Content” Build Action (previously it was None) and in doing so, the web.config file was copied to the `obj\Release\AspnetCompileMerge\Source` directory. Now the C# 6 syntax is recognized.

      It is worth noting that I have the following tag changes from this stackoverflow answer:

    • August 16, 2016 at 10:45 pm

      Ryan, I have used precompiled views only on compute instances as most of my Azure customers use these due to need for more control over VM-s where applications run. With compute instances we have had no problems this far.

    • January 7, 2017 at 11:04 pm

      Thanks for this great post.

      Any chance you call tell me how to precompile views via msbuild? You mention the commandline tools. I’m using Team Services for building and deploying the application. So I’m hoping for something a little easier.

      Maybe I’ll have to switch to ‘web deploy’ in the Azure RM step. And set some parameters that way. I already used Visual Studio to create a pubxml file. Will probably look into that tomorrow.

    • January 25, 2017 at 3:55 pm

      You mentioned precompile views in Azure but you didn’t explain how. This cannot be done using the standard publishing tool under Visual Studio.

    • January 25, 2017 at 4:06 pm

      Firas, it is standard publish dialog of Visual Studio and view compilation settings apply no matter where you publish your application – be it Azure or some other target environment like file system.

      On Azure we really need it because just in time compilation of views can be very slow due to slow disks that are cheaper than SSD-s.

    • January 25, 2017 at 6:07 pm

      Thanks Gunnar. This option is not supported if you are deploying ASP.NET MVC project to web role. I read a lot about it and eventually the tool doesn’t support it.

      If this is something you believe it exists there, I would appreciate it if you can share with me the steps to do it.

    • January 25, 2017 at 10:36 pm

      If VS deploys web role different from what regular web application publishing does then you can use dirty trick like described here: This way you get all files to package that are created during usual web application publishing.

      Actually the compiling of views is command that is given to MSBuild during publishing. You can also write DOS or PowerShell script for site deployment and call MSBuild there with correct parameters.

      Publishing with built views works also well for Azure app services (previous web sites service). I can confirm it.

    • June 16, 2017 at 12:50 pm

      This is a great article. So simple and a MASSIVE increase in performance of my Azure hosted Web site. Prior to this I was seriously thinking of moving to a different platfrom.

    • June 16, 2017 at 1:17 pm

      Building of views is slow and if it happens in server then also the first request is very slow. In case of Azure web sites consider also slow disks that are okay for web because once application is loaded it doesn’t make any remarkable disk I/O.

    • June 20, 2019 at 8:06 pm

      This was very helpful! Thank you for maintaining this content.

    Leave a Reply

    Your email address will not be published. Required fields are marked *