MVCBuildViews not working correctly

33,857

Solution 1

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.

Solution 2

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>

Solution 3

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.

Solution 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>

Solution 5

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>
Share:
33,857
Scott
Author by

Scott

C# programmer

Updated on July 09, 2022

Comments

  • Scott
    Scott almost 2 years

    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?

  • John Doe
    John Doe about 13 years
    I tried this, but alas it did not work for me. I will try some other solutions below in hopes of success.
  • mxmissile
    mxmissile over 12 years
    This works, but every time you restart VS2010 you have to delete the folder again.
  • John B
    John B over 12 years
    YES, THANK YOU. I added this to my .proj file: <BaseIntermediateOutputPath>..\bin</BaseIntermediateOutputPa‌​th> so now the obj folder is at the solution level, and the build issues are gone!!!
  • John B
    John B over 12 years
    See Joe Cartano's answer below for a permanent solution: stackoverflow.com/questions/4725387/…
  • MordechayS
    MordechayS over 11 years
    <MvcBuildViews>true</MvcBuildViews> inside your csproj file can cause this on build servers (the setting tells visual studio to compile the views)
  • Rob Church
    Rob Church almost 11 years
    To save following the link, the <BaseIntermediateOutputPath> element can be placed directly underneath the <MvcBuildViews> in the project file. It might be more correct to name your temp directory "obj" instead of "bin" but it doesn't really matter.
  • Jakob Olsen
    Jakob Olsen almost 9 years
    This solution worked for me too. Running VS2013 and MVC5
  • Jesper Mygind
    Jesper Mygind almost 8 years
    Or if you prefer to simply remove the entire obj\Debug (or obj\Release) folder, just add the following one-liner before <AspNetCompiler>: <RemoveDir Directories="$(BaseIntermediateOutputPath)\$(Configuration)" />
  • Worthy7
    Worthy7 about 7 years
    In general, that <Target Name="MvcBuildViews" AfterTargets="AfterBuild" Condition="'$(MvcBuildViews)'=='true'"> <AspNetCompiler VirtualPath="temp" PhysicalPath="$(WebProjectOutputDir)" /> </Target> code completely stopped my compiling from working except for the first time. Lots of wasted hours waiting for that recompile..It solved my problem
  • Petter Brodin
    Petter Brodin over 6 years
    @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"
  • Palec
    Palec about 6 years
    This is an implementation of the accepted answer by Nick Canzoneri.