views:

5528

answers:

4

I want an msbuild task to compile the views so I can see if there are compile time errors at well... compile time. Any ideas?

+4  A: 

You can use aspnet_compiler for this:

C:\Windows\Microsoft.NET\Framework\v2.0.50727\aspnet_compiler -v /Virtual/Application/Path/Or/Path/In/IIS/Metabase -p C:\Path\To\Your\WebProject -f -errorstack C:\Where\To\Put\Compiled\Site

where "/Virtual/Application/Path/Or/Path/In/IIS/Metabase" is something like this: "/MyApp" or "/lm/w3svc2/1/root/"

Also there is a AspNetCompiler Task on MSDN, showing how to integrate aspnet_compiler with MSBuild:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"&gt;
    <Target Name="PrecompileWeb">
        <AspNetCompiler
            VirtualPath="/MyWebSite"
            PhysicalPath="c:\inetpub\wwwroot\MyWebSite\"
            TargetPath="c:\precompiledweb\MyWebSite\"
            Force="true"
            Debug="true"
        />
    </Target>
</Project>
maxnk
This is out of date, see an excerpt from the readme doc below.
JarrettV
+1 for giving me the command line
Andrew Bullock
+3  A: 

Next release of ASP.NET MVC (available in January or so) should have MSBuild task that compiles views, so you might want to wait.

See announcement

bh213
I was just going to say that! :)
Haacked
+5  A: 

Also, if you use Resharper, you can active Solution Wide Analysis and it will detect any compiler errors you might have in aspx files. That is what we do...

bh213
It's true it works for aspx files, but the solution-wide analysis does not include ascx files (user controls)
mookid8000
I believe it does in R# 5, but it's a huge resource hog for large projects (even on my 16GB home machine it's not worth using).
Andrew
@Andrew / @mookid8000 -- R# will also catch errors that the compiler won't, such as missing/incorrect views and actions. R# will slow your PC down a bit (I find it fine on a large-ish project with 4GB ram and a hyperthreaded CPU) but I easily make back the time I spend waiting for it, and I end up doing fewer operations on my code as R# provides higher level operations that batch together the many steps I'd have to take to achieve the same task manually. Your project must be huge!
Drew Noakes
+68  A: 

From the readme word doc for RC1 (not indexed by google)

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"&gt;
  <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 <PropertyGroup> element: <MvcBuildViews>true</MvcBuildViews>
  3. At the end of the project file, uncomment the <Target Name="AfterBuild"> element and modify it to match the following

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

JarrettV
If this should not work for your project, check if there isn't an <MvcBuildViews>false</MvcBuildViews> somewhere in your project file. It was overriding the new <MvcBuildViews> element I added on top of it.
Adrian Grigore
Any way to get this to work with Web Forms?
mxmissile
@mxmissile: Scott Guthrie recommended adding a Web Deployment Project to your solution to get this sort of support in Web Application Projects: http://weblogs.asp.net/scottgu/archive/2006/09/22/Tip_2F00_Trick_3A00_-Optimizing-ASP.NET-2.0-Web-Project-Build-Performance-with-VS-2005.aspx
Zhaph - Ben Duguid
Make sure that EnableUpdateable is set to false or else the views wont be precompiled. <EnableUpdateable>false</EnableUpdateable><MvcBuildViews>true</MvcBuildViews> (http://devcarl.posterous.com/dont-combine-enableupdateable-and-mvcbuildvie)
Carl Hörberg