Using MvcBuildViews to force MVC Views to Compile

Tags: MVC, ASP.NET

In Visual Studio 2012, C# code inside MVC views does not get compiled in the normal build process.  This can lead to runtime bugs... yucky!

Force It

Thankfully, you can force Visual Studio to compile your views with a handy property attribute.  

This is how I did it:

  1. Create a new Build Configuration called DebugBuildViews
  2. Right click my project in Solution Explorer and Unload Project
  3. Right click my project in Solution Explorer and Edit MyProject.proj
  4. Navigate to the Property Group for me new DebugBuildViews Build Configuration (shown below)
  5. Add the following xml:  <MvcBuildViews>true</MvcBuildViews>
  6. Save the project file
  7. Right click my project in Solution Explorer and Reload Project
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'DebugBuildViews|AnyCPU'"> 
    <MvcBuildViews>true</MvcBuildViews>
    <DebugSymbols>true</DebugSymbols>
    <OutputPath>bin\</OutputPath>
    <DefineConstants>DEBUG;TRACE</DefineConstants>
    <DebugType>full</DebugType>
    <PlatformTarget>AnyCPU</PlatformTarget>
    <ErrorReport>prompt</ErrorReport>
    <CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
  </PropertyGroup>

Now I can switch to this new DebugBuildViews build config when I want views to compile and switch back to my Debug config when I don't.

No Comments

Add a Comment