配置文件 XDT 转换 InsertIfMissing 正在跳过子项

fur*_*ier 3 xml asp.net web-config transformation nuget

我在进行配置转换、在 nuget 包安装上添加应用程序设置(其中元素appSetting可能存在或不存在)时遇到问题。

我想要发生的事情:

  • appSetting元素不存在
    • 插入appSetting元素及其子元素
  • appSetting元素存在
    • 如果缺少子项,请插入

我只会让其中一个工作,但不会同时工作。

web.config.install.xdt

<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <appSettings xdt:Transform="InsertIfMissing" >
    <add key="Swagger.Contact.Name" value="Some Name" xdt:Transform="InsertIfMissing" />
    <add key="Swagger.Contact.Email" value="some@email.address" xdt:Transform="InsertIfMissing" />
  </appSettings>
</configuration>
Run Code Online (Sandbox Code Playgroud)

实施例1

之前的web.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.5.2" />
    <httpRuntime targetFramework="4.5.2" maxRequestLength="51200" />
    <customErrors mode="Off" />
  </system.web>
</configuration>
Run Code Online (Sandbox Code Playgroud)

appSettings转换前不存在的元素。

web.config之后

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.5.2" />
    <httpRuntime targetFramework="4.5.2" maxRequestLength="51200" />
    <customErrors mode="Off" />
  </system.web>
  <appSettings>
    <add key="Swagger.Contact.Name" value="Some Name" />
    <add key="Swagger.Contact.Email" value="some@email.address" />
  </appSettings>
</configuration>
Run Code Online (Sandbox Code Playgroud)

实施例2

之前的web.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.5.2" />
    <httpRuntime targetFramework="4.5.2" maxRequestLength="51200" />
    <customErrors mode="Off" />
  </system.web>
  <appSettings>
    <add key="Other.Key" value="With Some Value" />
  </appSettings>
</configuration>
Run Code Online (Sandbox Code Playgroud)

appSettings转变前存在的元素。

web.config之后

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.5.2" />
    <httpRuntime targetFramework="4.5.2" maxRequestLength="51200" />
    <customErrors mode="Off" />
  </system.web>
  <appSettings>
    <add key="Other.Key" value="With Some Value" />
  </appSettings>
</configuration>
Run Code Online (Sandbox Code Playgroud)

示例 2 中没有任何反应,因为appSettings元素已经存在,我希望它仍然评估其子元素并插入它们(如果它们不存在),但似乎它们只是被忽略。我可以使用该属性的任何其他值xdt:Transform,或者有任何其他技巧来解决此问题吗?

The*_*mer 5

不久前我也遇到过类似的问题。我应用的解决方法是在 XDT 文件中包含两个条目<appSettings>,一个用于检查它是否不存在,如果存在,则继续插入它。<appSettings>另一个是元素已经存在的情况。以下是帮助您解决问题的简短片段:

<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
    <appSettings xdt:Transform="InsertIfMissing">
    </appSettings>
    <appSettings>
      <add key="Swagger.Contact.Name" value="Some Name" xdt:Transform="InsertIfMissing" />
      <add key="Swagger.Contact.Email" value="some@email.address" xdt:Transform="InsertIfMissing" />
    </appSettings>
</configuration>
Run Code Online (Sandbox Code Playgroud)

让我知道这是否适合您。