Tuesday, February 24, 2009

ASP.NET MVC RC Compiler Post-Build Step

I was reading through the ASP.NET MVC RC Release Notes and saw a section that could be easy to skip over, but will help me out considerably on my MVC project.

Previously, I've been burned by my Views having compile time errors, but not catching them until runtime. You can now add a new <MvcBuildViews /> tag to your MVC project file to detect these errors at compile time!

The following is a snippet from the ASP.NET MVC RC Release Notes document...

ASP.NET Compiler Post-Build Step

Currently, errors within a view file are not detected until run time. To let you detect these errors at compile time, ASP.NET MVC projects now include an MvcBuildViews property, which is disabled by default. To enable this property, open the project file and set the MvcBuildViews property to true, as shown in the following example:

<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <MvcBuildViews>true</MvcBuildViews>
  </PropertyGroup>

Note: Enabling this feature adds some overhead to the build time.

You can update projects that were created with previous releases of MVC to include build-time validation of views by performing the following steps:

  1. Open the project file in a text editor.
  2. Add the following element under the top-most element:

    <MvcBuildViews>true</MvcBuildViews>
    
  3. At the end of the project file, uncomment the element and modify it to match the following example:

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

I just started upgrading my MVC project to the RC and having this Post-Build Step turned on really helped during the upgrade process.

2 comments:

  1. Great tip, thanks. I am sure it will come in handy for me as I am just starting an MVC project.

    ReplyDelete
  2. This seems not to have had any effect on my build. Can you please give a concrete example of a view error which wouldn't normally be picked up but is with this change?
    -matt

    ReplyDelete