Sunday, March 27, 2011

Using MvcBuildViews with One-click Publish and Deployment Packages

ASP.NET MVC Web Application project files (MVC 2+) have the ability to compile your view code when the rest of your application is compiled. Enabling this functionality requires manually modifying the project file. Read Compiling Views in ASP.NET MVC for a quick overview of how this can be achieved.

This is a great way of catching basic coding errors before the affected view is reached by a browser. It’s exacly the kind of thing you want to include in your build process – which might also include the creation of a deployment package the new MSDeploy-based packing in Visual Studio 2010.  Unfortunately there is a conflict between MvcBuildViews and Deployment Packages. Fortunately the fix is simple enough, just add some dependencies to the MvcBuildViews target so that any leftover packaging files are cleaned up before the MvcBuildView target executes.

<Target Name= "MvcBuildViews"
        AfterTargets= "AfterBuild "
        Condition= "'$(MvcBuildViews)'=='true'"
        DependsOnTargets= "CleanWebsitesPackage;CleanWebsitesPackageTempDir;CleanWebsitesTransformParametersFiles;"> 
    <AspNetCompiler VirtualPath= "temp " PhysicalPath= "$(MSBuildProjectDirectory) " /> 
</Target>