【问题标题】:ASP.NET: xdt:Transform does not insert new section in web.config fileASP.NET:xdt:Transform 不在 web.config 文件中插入新部分
【发布时间】:2020-03-25 02:34:06
【问题描述】:

我正在使用 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 中运行上述内容,我会收到以下错误:

&lt;error&gt;No element in the source document matches '/configuration/appSettings/add'&lt;/error&gt;

我做错了什么?如何确保添加了我的新部分?

【问题讨论】:

    标签: c# asp.net web-config visual-studio-2019 web-config-transform


    【解决方案1】:

    我似乎找到了问题所在。 xdt:Transform="Insert" 命令需要在定义 &lt;appSettings&gt; 部分时指定,而不是在其中:

      <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>
    

    【讨论】:

    • 不,真正的问题是您的源 (web.config) 没有一个 部分。因此,转换过程不知道如何将孩子插入 appSettings。
    猜你喜欢
    • 1970-01-01
    • 2018-09-07
    • 1970-01-01
    • 2013-05-09
    • 1970-01-01
    • 1970-01-01
    • 2014-07-05
    • 2010-10-16
    • 1970-01-01
    相关资源
    最近更新 更多