发布不会转换web.config文件吗?

16

我制作了一个web.config文件(完整文件,它不显示XML错误)

<?xml version="1.0"?>
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
  <configSections>
      ...
      <location path="." inheritInChildApplications="false">
        <connectionStrings>
          <add name="ElmahLog" connectionString="data source=~/App_Data/Error.db" />
          <add name="database" connectionString="w" providerName="System.Data.EntityClient"/>
        </connectionStrings>
      </location>
  ...

使用一个转换文件 (web.Staging.config)

<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <connectionStrings>
    <add name="database"
      connectionString="c"
      providerName="System.Data.EntityClient"
      xdt:Transform="SetAttributes" xdt:Locator="Match(name)" />
  </connectionStrings>
  <system.web>
    <compilation xdt:Transform="RemoveAttributes(debug)" />
    <customErrors defaultRedirect="error.aspx"
      mode="RemoteOnly" xdt:Transform="Replace">
    </customErrors>
  </system.web>
</configuration>

我正在以暂存模式发布(右键单击网站 > 发布 > 方法:文件系统...)

------ Build started: Project: Drawing, Configuration: Staging Any CPU ------
  Drawing -> D:\Project\bin\Staging\Drawing.dll
------ Build started: Project: MySystem, Configuration: Staging Any CPU ------
  MySystem -> D:\Project\bin\Staging\MySystem.dll
...

但当我查看输出文件夹中的web.config时,它没有被更改。

我在构建日志中发现了以下内容:

D:\Project\Web.Staging.config(3,2): Warning : No element in the source document matches '/configuration'
D:\Project\Web.Staging.config(3,2): Warning : No element in the source document matches '/configuration'
D:\Project\Web.Staging.config(3,2): Warning : No element in the source document matches '/configuration'
Transformed web.config using Web.Staging.config into obj\Staging\TransformWebConfig\transformed\web.config.

可能是什么问题?我这样做对吗?


当你发布时,它是构建为“staging”还是“release”? - Jimmy
@Jimmy,根据构建日志显示,它正在编译Staging。虽然有一个用于Release的转换文件,但它也无法正常工作。 - BrunoLM
你是否看到一条消息,指示它正在执行转换?像我的一样,会显示:“使用Web.Beta.config 转换 Web.config,结果输出在 obj\Beta\TransformWebConfig\transformed\Web.config.” - CodingGorilla
@Coding 是的,但之前有一些错误,我刚刚注意到了... 我更新了我的问题... - BrunoLM
你能否发布有问题的 web.config 文件的第一部分? - Jeff Parker
8个回答

23

回答有点晚了,但也许我能帮助避免某些人头疼的问题。在 Visual Studio 2013 中,有两个地方可以选择构建和部署的配置。配置管理器,然后再次使用发布 Web,在向导中的第三步"设置"中允许您选择要使用的配置。如果您不选择新配置,它将使用所选配置的转换而不是您的配置。


5
你确实帮我省去了一件让我头痛的事情。 - izb
2
这是真正的答案。 - Roy Oliver
1
在发布设置中,我的配置是正确的(发布),但转换没有起作用。我将其更改为debug,然后保存,再将其更改回release,它就开始正常工作了。感谢指导! - puddleglum

17

我发现了两件事:

  • 您无法在<configuration>标签上设置命名空间(例如:<location path="." inheritInChildApplications="false">
  • 您必须关注变换文件中的正确层次结构。

例如:

<configuration>
  <location>
    <connectionStrings>

不要使用

<configuration>
  <connectionStrings>

关于 xmlns:,实际上你可以使用它,但是你必须将它添加到转换根标记中。没有 xmlns,它也可以工作,但是 VS 无法识别 inheritInChildApplications 属性。 - amartynov

6

请确保 Web.Config 文件的属性中 Build Action 被设置为 Content

如果构建操作被设置为 None,即使它被复制到输出目录,也不会进行转换。


3

确保在尝试添加的节不存在于输出中时,包括InsertIfMissing

<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <location>
    <system.webServer>

      <security xdt:Transform="InsertIfMissing">
        <requestFiltering allowDoubleEscaping="true" />
      </security>

    </system.webServer>
  </location>
</configuration>

0

不要忘记从原始的 "web.config" 中复制 "configuration" 的所有其他属性,因为似乎 VS2012 不会自动执行此操作,当然也不会有匹配项...


3
不对。您可以指定仅更改的属性,并指示转换仅更新属性(而不是替换元素): <compilation debug="false" xdt:Transform="SetAttributes"/> - Josh M.

0
我按照以下步骤解决了这个问题。感谢 @michaelhawkins 指出正确的方向。您需要确保在两个地方将配置更改为发布状态。

enter image description here

然后右键单击您的项目并选择“属性”。如果不起作用,请尝试在CPU架构中选择x86。

enter image description here


0

虽然回答有点晚,但这可能会对某些人有所帮助。

我意识到,如果您在同一个解决方案中有两个网站,当您尝试发布其中一个时,如果您只为两个项目之一配置了一个配置,则转换可能无法正常工作。

我的一个网站总是在转换,但另一个有时是有时不是。

例如,我在解决方案中使用了“自动”配置,并为两个网站都使用了web.Auto.config。

我通过创建一个具有不同名称的新配置 - “AutoAdmin” - 为第二个项目创建其web.AutoAdmin.config文件来解决了这个问题。当我再次发布它时,转换终于发生了。


0
@Karthikeyan VK,你的帖子解决了我的问题。虽然我在发布配置文件中选择了生产配置,但在配置管理器中它被设置为开发环境,因此它没有转换我的设置。
微软需要修复这个漏洞。一旦你在发布配置文件中选择了一个配置,它应该自动更新配置管理器。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接