Geeks With Blogs

News Please visit me at my new blog!!

profile for Aligned at Stack Overflow, Q&A for professional and enthusiast programmers
"free in Christ Jesus from the law of sin and death." Romans 8:2 (ESV) Check out the Falling Plates video on YouTube.
more about the Gospel
And then listen to Francis Chan speaking at LifeLight in SD.

Programming and Learning from SD

One part of MVC Views that can cause problems is that the cshtml can cause run-time errors. This can lead to bugs in Production or found by QA that could be avoided if it was built when you build the project. Pre-compiling can be turned on to avoid those problems. You add this in the project file of your web application, by setting the <MvCBuildViews>true</MvcBuildViews> in the .csproj. See for more details.

Another reason to pre-compile views is to avoid the initial hit of compiling when the website is first hit after publishing. It also ensures that changes can’t be made on the server. I did this in ASP.Net Web Forms using the Web Deploy Project back in the day. (more information) The <MVCBuildViews> doesn’t create place holders on deployment as I first expected. A co-worker pointed out that we could use the aspNet compiler in a post build or in a command-line.

.\aspnet_compiler.exe -v / -p C:\testdeploy c:\testdeploy2

Then we found the option in the web project’s publish Settings in Visual Studio 2012 (I have Premium).


After deployment, the views are just place holders. Opening it up in notepad they say “This is a marker file generated by the precompilation tool, and should not be deleted!”. You’ll also see many .compiled files in the bin directory. For example: _contactinfo.cshtml.c4daf601.compiled

Another reference:

Update:  do this in a VSTS build:

There are good and similar answers on Stack Overflow:

Posted on Tuesday, May 28, 2013 12:28 PM MVC | Back to top

Comments on this post: Pre-Compiling your MVC Views

# re: Pre-Compiling your MVC Views
Requesting Gravatar...
or you can use
Left by Anon on May 29, 2013 8:31 AM

Your comment:
 (will show your gravatar)

Copyright © Aligned | Powered by: