The Aspnet Compiler Build Task in Visual Studio 2010 ASP.Net MVC 2 Projects

The Aspnet Compiler Build Task in Visual Studio 2010 ASP.Net MVC 2 Projects

  • Comments 1

If you crack open the project file in an ASP.Net MVC 2 application (in notepad or unload your project and then click “Edit *.proj”), you will notice an interesting line.

<MvcBuildViews>false</MvcBuildViews>

This is an option to enable a post build task that will run the Asp_net compiler. This command will compile your aspx pages and report any errors in the pages at design time. Ordinarily the compiler runs before your site loads for the first time after making a change. This is why you can debug a website or web application with errors in the markup and not see an error until actually running your site.

So why is the option set to false by default? I obviously want errors at design time before I run the site!

Well, if you turn on the option you will probably find that it takes longer to build your site. How much longer really depends but it will probably be a noticeable increase because each .aspx page is compiled into a separate dll by the Asp_net compiler. You can poke around “C:\Windows\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files” if you want to take a look for yourself.

Okay, I still want to see errors in my markup pages at design time, what else do I need to know?

Later in the project file, you will find the task that is enabled when you set MvcBuildViews to true:

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

  • This build task will run the Asp_net compiler on the root of your app. This can cause a problem if you publish or package your app with MSDeploy as multiple web.configs will exist in your application’s folder structure (in the obj folder by default), which can confuse the compiler. To mediate this, you should add the following property to your project file, preferably below the MvcBuildViews property:

<MvcBuildViews>false</MvcBuildViews>                                                                                                                                                                                                                                                                                                     <BaseIntermediateOutputPath>[SomeKnownLocationIHaveAccessTo]</BaseIntermediateOutputPath>

This will cause all of the files generated by MSDeploy to be output to whatever location is specified. This is preferable to generating files in the same folder as your application but cannot be the default behavior because there is no obvious known location for the BaseIntermediateOutputPath.

  • One other thing to note is that this build task will always run the 4.0 version of the ASP_Net compiler. If you would like to run the 2.0 version of the compiler (you are targeting 3.5 rather than 4.0), set the ToolPath property of the build task to point to the 2.0 version of the .exe like so:

<Target Name="AfterBuild" Condition="'$(MvcBuildViews)'=='true'">
    <AspNetCompiler ToolPath="C:\Windows\Microsoft.NET\Framework\v2.0.50727"  VirtualPath="temp"  PhysicalPath="$(ProjectDir)\..\Web" />
  </Target>

This will call the 2.0 version of the compiler to build your site just like it would have been called when you first ran your 3.5 ASP.Net MVC 2 application.

Last but not least, check out Web Deployment Projects.

Web Deployment Projects is an add-in that runs the Asp_net compiler and merge tool. It automatically handles setting the correct ToolPath and also places the output from publishing and packaging your ASP.Net MVC application in a sibling directory so you will not get build errors after publish. There are a lot more options you can configure if needed but even out of the box it is an ideal solution for building ASP.Net MVC view pages without having to make any project file edits. Here is a link with more information on how to get started if you are interested: http://blogs.msdn.com/webdevtools/archive/2010/04/14/visual-studio-2010-web-deployment-projects-beta-avail-now.aspx

Hope this helps you develop your ASP.Net MVC 2 applications in Visual Studio 2010!

Joe Cartano | Visual Web Developer

Leave a Comment
  • Please add 5 and 4 and type the answer here:
  • Post
  • Thanks for this. I struggle to understand why this isn't on by default in the era of multi-core processors. It always confounded me that you go to the trouble of creating a strongly typed view and VS wouldn't catch typos in markup or code logic or bogus refrence such as when a property is renamed or deleted.

Page 1 of 1 (1 items)