سؤال

I'm using MSDeploy (Web Deploy) to publish my MVC application, which works great. The only issue is that it puts all the compiled files, along with an extra web.config in the obj directory, so the next time I try to build I get the following error:

Error   1   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.  

If I delete obj the problem goes away, but it is annoying. I could put a post-build task to do this for me, but it feels wrong. Shouldn't there be a better way?

هل كانت مفيدة؟

المحلول

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:

  1. 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.
  2. 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.

نصائح أخرى

I had my web.config property "Copy To Output Directory" set to "Copy Always". Setting it back to "Do not copy" resolved it for me.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top