配置文件XDT转换中的InsertIfMissing跳过了子元素。

3

我在进行配置转换、添加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>

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

在转换之前,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>

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

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>

在示例2中没有任何事情发生,因为appSettings元素已经存在,我希望它仍然评估其子元素并在不存在时插入这些元素,但似乎它们只是被忽略了。是否有其他值可以用于属性xdt:Transform,或者有其他方法可以解决这个问题?

1个回答

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>

如果这对您有用,请告诉我。

我考虑过这个问题,但没有尝试。等我有时间了再试试吧,因为我现在改用 XML 转换了,文件名类似于 web.config.transform。不过它似乎会插入重复的条目,所以如果这个方法行得通的话,我就改用这种方式了。 :) - furier
从我所做的有限测试来看,它似乎可以正常工作。 :) - furier

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