VS2013 does not compile ASP.NET MVC5 views

Right click your web project in the Solution Explorer. Click Unload Project. Right click the project and click Edit <projname>.csproj.

Make sure you have this element (add it if it doesn’t exist).

<Project>
  <PropertyGroup>
    <MvcBuildViews>true</MvcBuildViews>
  </PropertyGroup>
</Project>

Scroll down to the bottom. You should see some comment “To modify your build process, add your task inside one of the targets below and uncomment it.” Below that, add this markup:

<Target Name="MvcBuildViews" AfterTargets="AfterBuild" Condition="'$(MvcBuildViews)'=='true'">
    <ItemGroup>
      <ExtraWebConfigs Include="$(BaseIntermediateOutputPath)\**\web.config" />
      <ExtraPackageTmp Include="$([System.IO.Directory]::GetDirectories(&quot;$(BaseIntermediateOutputPath)&quot;, &quot;PackageTmp&quot;, System.IO.SearchOption.AllDirectories))" />
    </ItemGroup>
    <Delete Files="@(ExtraWebConfigs)" />
    <RemoveDir Directories="@(ExtraPackageTmp)" />
    <AspNetCompiler VirtualPath="temp" PhysicalPath="$(WebProjectOutputDir)" />
  </Target>

(the above code should have a parent of the root <Project> node in case you don’t see the comment I mentioned)

Close the .csproj file, then right click your project in the Solution Explorer and click Reload Project.

This will add your views to the compilation step, and will stop your build if errors are found. I found this to be a good thing, because without it I sometimes don’t notice errors in the Error List until I’ve deployed my site and then manually hit them. Be warned, it will add some time to your build step, significantly slowing it down. Depending on what you’re trying to achieve, you may want to selectively enable/disable it to achieve a rapid build -> test workflow.

Inspiration for this answer was taken from Chris Hyne’s answer to MVCBuildViews not working correctly and Can Razor views be compiled?.

Leave a Comment