Tag Archives: mvc

allowDefinition=’MachineToApplication’ error after enabling MVC compile-time view checking

Two posts in one day?  Yesh… I’m just trying to make up for my extended hiatus from blogging!

Actually, this post supplements the previous one, but I think it is the more important of the two since I don’t believe there is a complete solution to this problem is anywhere else on the interweb (except for my answer on stackoverflow, which has ZERO upvotes… not that I’m counting LOL).

Here’s our scenario:

  1. The developer wants compile-time checking on views, so they set MvcBuildViews=true.
  2. The application builds fine, UNTIL they publish the project.
  3. Subsequent attempts to build the project result in a compile-time error: It is an error to use a section registered as allowDefinition='MachineToApplication' beyond application level. This error can be caused by a virtual directory not being configured as an application in IIS.

So what causes this issue? When the project is published the compiler, by default it uses <project-dir>\obj\ to place copies of the source files that it will work with. Unfortunately, these files are not automatically deleted when publishing is complete. The next time the developer compiles the project with MvcBuildViews=true, it will error out because the aspnet compiler includes the obj\ folder during compilation, since it is underneath the <project-dir> folder.

So how do we fix this? Well, you have four options:

  1. Set MvcBuildViews=false. I don’t really consider this a solution, so let’s move on.
  2. Delete the files in <project-dir>\obj\. Works, but can be a hassle since it has to be done after every publish.
  3. Change the path that publishing uses as an intermediate directory through the use of the <BaseIntermediateOutputPath> property in your project config file. See this link.
  4. Add a new section in your project config file that deletes the offending files for you on build (reference Microsoft Connect). I’ve even made it easy for you, just copy and paste:
    <_EnableCleanOnBuildForMvcViews Condition=" '$(_EnableCleanOnBuildForMvcViews)'=='' ">true</_EnableCleanOnBuildForMvcViews>
    <Target Name="CleanupForBuildMvcViews" Condition=" '$(_EnableCleanOnBuildForMvcViews)'=='true' and '$(MVCBuildViews)'=='true' " BeforeTargets="MvcBuildViews">
        <_TempWebConfigToDelete Include="$(BaseIntermediateOutputPath)**\Package\**\*" />
        <_TempWebConfigToDelete Include="$(BaseIntermediateOutputPath)**\TransformWebConfig\**\*" />
        <_TempWebConfigToDelete Include="$(BaseIntermediateOutputPath)**\CSAutoParameterize\**\*" />
        <_TempWebConfigToDelete Include="$(BaseIntermediateOutputPath)**\TempPE\**\*" />
    <Delete Files="@(_TempWebConfigToDelete)"/>

My recommendation would be to use either option 3 or 4.


ASP.NET MVC compile-time view checks in Visual Studio 2012

Anyone who has worked with MVC has suffered through runtime view errors.  This is because views don’t get compiled until IIS renders them.  In order to check for successful compilation, you can:

  1. Manually visit every view in the browser (easy but tedious)
  2. Create automated UI tests (harder)
  3. Turn on compile-time view checks (just easy)!

Since I like just easy, I’m going to show you how to turn on compile-time view checks:

  1. Unload the project (right-click on the project in Solution Explorer and select Unload Project
  2. Right-click the project again and select Edit *.csproj
  3. In the PropertyGroup section, add: <MvcBuildViews>true</MvcBuildViews>
  4. Save the csproj file and reload the project
  5. Eat a banana (optional)

Now, when running the build, visual studio will compile all of your views as well, and identify any errors.