I assume that you're using MvcBuildViews to compile your site during a build in Visual Studio, as the default behavior for precompile during publish is to copy site contents to a temp folder and precompile that folder (so as to avoid issues like obj
or other excluded files).
The easiest workaround, documented here, is to relocate your obj
directory elsewhere, like this:
<BaseIntermediateOutputPath>Somewhere\Not\Under\Project\Directory</BaseIntermediateOutputPath>
Longer explanation:
MvcBuildViews invokes the ASP.NET compiler as such in the project file:
<Target Name="MvcBuildViews" AfterTargets="AfterBuild" Condition="'$(MvcBuildViews)'=='true'">
<AspNetCompiler VirtualPath="temp" PhysicalPath="$(WebProjectOutputDir)" />
</Target>
The complier by default will compile all contents of the site below that root folder specified. The error stems from when obj\web.config
contains things that are only allowed in the site root web.config
(since that's where it was copied from).
2 other workarounds:
- Don't use MvcBuildViews. Ever since Web Deployment Projects were dropped in favor of supporting WebDeploy natively for all web projects, you can configure pre-compilation before publishing. This does lose the compiler checking on each build.
- As of .NET 4.5, the aspnet_compiler.exe now supports a -x flag to exclude a specific directory from precompilation. Unfortunately, this flag never made it onto the AspNetCompilerTask, so you would have to instead rewrite the target to invoke the compiler and pass any necessary arguments yourself.