While updating your website, you added a few new actions and views, unit tested it, and are now ready to check it in for the testers. All set, right? Wait – have you tried compiling the C#/VB code inside the views? What? That’s not happening automatically? Nope!

Actually, in all versions of ASP.NET MVC with VS 2010 and newer, you must change a project property to have it compile the C# or VB code inside the views as well. Microsoft purposely disabled this functionality by default in order to save build time. After building your ASP.NET MVC views, you’ll probably want to change back to being disabled because of the increased build time required.

This is a property that must be manually set in the .csproj or .vbproj file. Thankfully, Visual Studio makes it easy to edit the project file and change this. Here are the steps to temporarily enable/disable this feature.

  1. With your ASP.NET MVC Web Application project open in Visual Studio, close all open windows by clicking Window | Close All Documents
    Compile1
  2. Unload the project from Visual Studio. This also unloads the .csproj or .vbproj file from Visual Studio’s control. Right-click the project in the Solution Explorer window and click Unload Project.
    Compile2
  3. Right-click the unloaded project in the Solution Explorer window and click Edit xxxx.csproj or xxxx.vbproj.
    Compile3
  4. Find the tag for MvcBuildViews and change it from false to true in all lowercase letters. Remember to save the .csproj or .vbproj file!
    Compile4
  5. Right-click the project in the Solution Explorer window and click Reload project. You’ll be asked if Visual Studio can close the file – Yes!
    Compile5
  6. Rebuild your project. Note that the build process usually takes significantly longer now that views are also being compiled.

Compile6

 

Some errors may appear because of bad code written to the views. Where did these come from? Fix those code errors in the views before turning of this feature. Best wishes and good luck!