54

So I edited my csproj file on an MVC 3 RTM application to set the following property:

<MvcBuildViews>true</MvcBuildViews>

This should cause my views to be complied during build and force a build error if my view is broken. This is the only change I made, however, when I try to build the application, I get the following 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.

The project compiles and runs successfully if I change back to false,

The following are the build tasks configured in the csproj file (these were never manually edited, they were added by Visual Studio 2010)

<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target> -->
<Target Name="MvcBuildViews" AfterTargets="AfterBuild" Condition="'$(MvcBuildViews)'=='true'">
  <AspNetCompiler VirtualPath="temp" PhysicalPath="$(WebProjectOutputDir)" />
</Target>

Am I missing something here? How do I get MVC 3 / Visual Studio 2010 configured correctly to validate my views at build time?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Scott
  • 13,735
  • 20
  • 94
  • 152
  • 1
    Your web.config has some invalid section defined in it. It's unfortunate the error message does not provide any details. Do you have anything suspicious in web.config? – marcind Jan 18 '11 at 18:48

6 Answers6

55

I had this problem a few days ago and I fixed it by deleting obj/Debug folder. Cleaning the project also works. I have no idea about the cause of the issue, though.

See Joe Cartano's answer for a more permanent solution.

Palec
  • 12,743
  • 8
  • 69
  • 138
Nick Canzoneri
  • 3,774
  • 1
  • 23
  • 17
  • 2
    I tried this, but alas it did not work for me. I will try some other solutions below in hopes of success. – MikeJ Feb 21 '11 at 14:49
  • This works, but every time you restart VS2010 you have to delete the folder again. – mxmissile Nov 02 '11 at 15:08
  • See Joe Cartano's answer below for a permanent solution: http://stackoverflow.com/questions/4725387/mvcbuildviews-not-working-correctly#4732019 – John B Nov 18 '11 at 13:40
  • `true` inside your csproj file can cause this on build servers (the setting tells visual studio to compile the views) – Chris S Nov 18 '12 at 23:57
38

This problem occurs when there is web project output (templated web.config or temporary publish files) in the obj folder. The ASP.NET compiler used isn't smart enough to ignore stuff in the obj folder, so it throws errors instead.

Another fix is to nuke the publish output right before calling <AspNetCompiler>. Open your .csproj and change this:

<Target Name="MvcBuildViews" AfterTargets="AfterBuild" Condition="'$(MvcBuildViews)'=='true'">
  <AspNetCompiler VirtualPath="temp" PhysicalPath="$(WebProjectOutputDir)" />
</Target>

to this:

<Target Name="MvcBuildViews" AfterTargets="AfterBuild" Condition="'$(MvcBuildViews)'=='true'">
  <ItemGroup>
    <ExtraWebConfigs Include="$(BaseIntermediateOutputPath)\**\web.config" />
    <ExtraPackageTmp Include="$([System.IO.Directory]::GetDirectories(&quot;$(BaseIntermediateOutputPath)&quot;, &quot;PackageTmp&quot;, System.IO.SearchOption.AllDirectories))" />
  </ItemGroup>
  <Delete Files="@(ExtraWebConfigs)" />
  <RemoveDir Directories="@(ExtraPackageTmp)" />
  <AspNetCompiler VirtualPath="temp" PhysicalPath="$(WebProjectOutputDir)" />
</Target>

That will delete all web.configs under \obj, as well as all PackageTmp folders under \obj.

UPDATE:

Even better, based off https://stackoverflow.com/a/48582282/8037 you can exclude the obj folder entirely. Apparently the <AspNetCompiler /> task doesn't have an exclude parameter, but if you switch to calling the aspnet_compiler .exe directly, you can exclude obj like this:

<Target Name="MvcBuildViews" AfterTargets="AfterBuild" Condition="'$(MvcBuildViews)'=='true'">
    <Exec Command="$(MSBuildFrameworkToolsPath)aspnet_compiler.exe -v temp -p $(WebProjectOutputDir) -x $(BaseIntermediateOutputPath)"/>
</Target>
Chris Hynes
  • 9,999
  • 2
  • 44
  • 54
  • This solution worked for me too. Running VS2013 and MVC5 – Jakob Olsen Jun 23 '15 at 12:33
  • Or if you prefer to simply remove the entire obj\Debug (or obj\Release) folder, just add the following one-liner before : – Jesper Mygind Jun 15 '16 at 11:28
  • In general, that ` ` code completely stopped my compiling from working except for the first time. Lots of wasted hours waiting for that recompile..[It solved my problem](http://stackoverflow.com/questions/42435121/why-wont-my-asp-mvc-views-build-the-second-time-round/42781599#42781599) – Worthy7 Mar 14 '17 at 09:02
22

When you get this error do you have another web.config file in your obj folder? If you are using MSDeploy this might help: MSDN Blog: The Aspnet Compiler Build Task in Visual Studio 2010 ASP.Net MVC 2 Projects, if not, maybe another web.config is being generated by some tool you are running.

DecimalTurn
  • 3,243
  • 3
  • 16
  • 36
Joe Cartano
  • 2,997
  • 3
  • 22
  • 40
  • 7
    YES, THANK YOU. I added this to my .proj file: `..\bin` so now the obj folder is at the solution level, and the build issues are gone!!! – John B Nov 18 '11 at 13:39
  • 1
    To save following the link, the `` element can be placed directly underneath the `` in the project file. It might be more correct to name your temp directory "obj" instead of "bin" but it doesn't really matter. – Rob Church Aug 02 '13 at 16:28
  • 1
    @JohnBubriski if you copy the code from your comment there's some weird unicode (I presume) issue causing the opening tag not to match the closing tag even if they look identical. The closing tag has some invisible character between the "a" and "t" in "Path" – Petter Brodin Oct 12 '17 at 08:42
4

This is what worked for me. Optionally, you may specify a condition with the configuration.

<Target Name="MvcBuildViews" AfterTargets="AfterBuild" Condition="'$(MvcBuildViews)'=='true'">
  <AspNetCompiler VirtualPath="temp" PhysicalPath="$(WebProjectOutputDir)" />
</Target>
<Target Name="AfterBuild" Condition="'$(Configuration)'!='Debug'">
  <RemoveDir Directories="$(BaseIntermediateOutputPath)" />
</Target>
arni
  • 2,152
  • 19
  • 14
2

This issue of Compile-time View Checking even though MvcBuildViews is set to 'true' is well-explained in the following MSDN blog:

https://blogs.msdn.microsoft.com/jimlamb/2010/04/20/turn-on-compile-time-view-checking-for-asp-net-mvc-projects-in-tfs-build-2010/

You could do the fix by editing .csproj file directly:

<PropertyGroup>
   <MvcBuildViews>true</MvcBuildViews>
</PropertyGroup>

 <Target Name="BuildViews" Condition="'$(MvcBuildViews)'=='true'" AfterTargets="Build">
   <Message Importance="normal" Text="Precompiling views" />
   <AspNetCompiler VirtualPath="temp" PhysicalPath="$(WebProjectOutputDir)" />
 </Target>
David
  • 239
  • 3
  • 10
2

A simple solution kinda compiled from the other answers here

You can simply remove the whole /obj folder like this:

<Target Name="MvcBuildViews" AfterTargets="AfterBuild" Condition="'$(MvcBuildViews)'=='true'">
    <RemoveDir Directories="$(ProjectDir)$(BaseIntermediateOutputPath)" /> <!--add this line-->
    <AspNetCompiler VirtualPath="temp" PhysicalPath="$(WebProjectOutputDir)" />
</Target>
Alex from Jitbit
  • 53,710
  • 19
  • 160
  • 149