简体   繁体   English

如何停止Visual Studio Web将web.config写入/ obj目录

[英]How to stop Visual Studio Web writing web.config to /obj directory

I'm using MSDeploy (Web Deploy) to publish my MVC application, which works great. 我正在使用MSDeploy(Web部署)发布我的MVC应用程序,效果很好。 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: 唯一的问题是,它将所有已编译的文件以及一个额外的web.config放在obj目录中,因此,下次我尝试构建时,出现以下错误:

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. 如果我删除obj,问题就消失了,但这很烦人。 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). 我假设您在Visual Studio的构建过程中使用MvcBuildViews来编译您的网站,因为发布期间预编译的默认行为是将网站内容复制到temp文件夹并预编译该文件夹(以避免obj或其他问题排除的文件)。

The easiest workaround, documented here , is to relocate your obj directory elsewhere, like this: 这里记录的最简单的解决方法是将obj目录重新定位到其他位置,如下所示:

<BaseIntermediateOutputPath>Somewhere\Not\Under\Project\Directory</BaseIntermediateOutputPath>

Longer explanation: 更长的解释:

MvcBuildViews invokes the ASP.NET compiler as such in the project file: MvcBuildViews会在项目文件中这样调用ASP.NET编译器:

<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). 该错误源于obj\\web.config包含仅在站点根目录web.config允许的内容(因为它是从那里复制的)。

2 other workarounds: 2种其他解决方法:

  1. Don't use MvcBuildViews. 不要使用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. 自从放弃 Web部署项目支持本机为所有Web项目提供本机支持以来,您就可以在发布之前配置预编译。 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. 从.NET 4.5开始,aspnet_compiler.exe现在支持-x标志,以从预编译中排除特定目录。 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. 不幸的是,此标志从未将其添加到AspNetCompilerTask上,因此您将不得不重写目标来调用编译器并自己传递任何必要的参数。

I had my web.config property "Copy To Output Directory" set to "Copy Always". 我将我的web.config属性“复制到输出目录”设置为“始终复制”。 Setting it back to "Do not copy" resolved it for me. 将其重新设置为“请勿复制”为我解决了该问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM