ASP.NET: xdt:Transform 不在 web.config 文件中插入新节

ASP.NET: xdt:Transform does not insert new section in web.config file

我正在使用 ASP.NET 4.7.2。我有以下 web.config 文件:

<?xml version="1.0"?>
<configuration>
  <connectionStrings>
    <add name="foo" connectionString="value"/>
  </connectionStrings>
  <system.web>
    <customErrors mode="Off"/>
  </system.web>
</configuration>

和以下 web.debug.config 文件:

<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <xdt:Import assembly="AppHarbor.TransformTester" namespace="AppHarbor.TransformTester.Transforms"/>

  <configSections xdt:Transform="MergeBefore(/configuration/*)" />

  <appSettings>
    <add key="key1" value="1" xdt:Transform="Insert"/>
  </appSettings>

</configuration>

当我在 Visual Studio 2019 上进行预览更改时,我没有看到添加 key1,如果我 运行 在 WebConfigTransformationTester 中添加上述内容,我会得到以下错误:

<error>No element in the source document matches '/configuration/appSettings/add'</error>

我做错了什么?我怎样才能确保添加了我的新部分?

我好像找到问题了。 xdt:Transform="Insert"命令需要在定义<appSettings>段时指定,而不是在里面:

  <appSettings xdt:Transform="Insert">
    <add key="key1" value="1"/>
  </appSettings>

我的转换现在是正确的:

<?xml version="1.0"?>
<configuration>
  <connectionStrings>
    <add name="foo" connectionString="value" />
  </connectionStrings>
  <system.web>
    <customErrors mode="Off" />
  </system.web>
  <appSettings>
    <add key="key1" value="1" />
  </appSettings>
</configuration>