ASP.NET MVC 1.0 AfterBuilding Views fails on TFS Build ASP.NET MVC 1.0 AfterBuilding Views fails on TFS Build asp.net asp.net

ASP.NET MVC 1.0 AfterBuilding Views fails on TFS Build


Actually, there's a better solution to this problem. I've tested it with VS/TFS 2010 but it should also work with VS/TFS 2008.

<Target Name="AfterBuild" Condition="'$(MvcBuildViews)'=='true'">  <AspNetCompiler VirtualPath="temp" PhysicalPath="$(WebProjectOutputDir)" /></Target>

I'm going to work with the MVC team to update their project template to to use this approach along with a custom target (rather than overriding AfterBuild).

I've published a blog post on How to Turn on Compile-time View Checking for ASP.NET MVC projects in TFS Build 2010.


The problem stems from the fact that the AspNetCompiler MSBuild task used within the AfterBuild target of an ASP.NET MVC project expects to reference the dll's in the bin folder of the Web project.

On a desktop build the bin folder is where you would expect it under your source tree.

However TFS Teambuild compiles the output of your source to a different directory on the build server. When the AspNetCompiler task starts it cannot find the bin directory to reference the required DLL and you get the exception.

Solution is to modify the AfterBuild target of the MVC Project to be as follows:

  <Target Name="AfterBuild" Condition="'$(MvcBuildViews)'=='true'">    <AspNetCompiler Condition="'$(IsDesktopBuild)' != 'false'" VirtualPath="temp" PhysicalPath="$(ProjectDir)\..\$(ProjectName)" />    <AspNetCompiler Condition="'$(IsDesktopBuild)' == 'false'" VirtualPath="temp" PhysicalPath="$(PublishDir)\_PublishedWebsites\$(ProjectName)" />  </Target>

This change enables you to compile Views on both the desktop, and the TFS build server.


Jim Lamb's solution didn't work for us when I built our web .csproj with

/p:UseWPP_CopyWebApplication=true;PipelineDependsOnBuild=False

because the target was being executed AfterBuild and the application has not been copied into the WebProjectOutputDir yet. (BTW, I pass those properties to the web project build cos I want the build to create a OutDir folder with only my binaries and cshtml files suitable for zipping, ie not an in-place build)

To get around this issue and honour the intent of his original target, I did the following:

<PropertyGroup>    <OnAfter_WPPCopyWebApplication>        MvcBuildViews;    </OnAfter_WPPCopyWebApplication></PropertyGroup><Target Name="MvcBuildViews" Condition="'$(MvcBuildViews)'=='true'">    <AspNetCompiler VirtualPath="temp" PhysicalPath="$(WebProjectOutputDir)" /></Target>