簡體   English   中英

Web.config轉換添加樹

[英]Web.config transforms add tree

我想在發布時將以下內容添加到Web配置中:

<system.webServer>
    <httpProtocol>
        <customHeaders>
            <add name="Strict-Transport-Security" value="max-age=16070400; includeSubDomains" xdt:Transform="Insert" />
        </customHeaders>
    </httpProtocol>
</system.webServer>

默認的Web配置中沒有任何自定義標頭,因此發布時會出現錯誤: No element in the source document matches '/configuration/system.webServer/httpProtocol/customHeaders'

我可以修復它,只需將空元素添加到web.config中,如下所示:

  <httpProtocol>
    <customHeaders>
    </customHeaders>
  </httpProtocol>

但是,這感覺不正確

有沒有更正確的方法在轉換上構建元素樹?

將空的<customHeaders>節點添加到web.config中是<customHeaders> ,因為您進行的轉換是插入<add .../>節點,而不是<customHeaders>節點。 它只能插入與該點匹配的位置。

要插入節點樹,請在XML中向上移動xdt:Transform="Insert" 如果從以下Web.config開始:

<?xml version="1.0">
<configuration>
  <system.webServer>
    <httpProtocol />
  </system.webServer>
</configuration>

並將其轉換為:

<?xml version="1.0">
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <system.webServer>
    <httpProtocol>
      <customHeaders xdt:Transform="Insert">
        <add name="Strict-Transport-Security" value="max-age=16070400; includeSubDomains" />
      </customHeaders>
    </httpProtocol>
  </system.webServer>
</configuration>

您最終將得到:

<?xml version="1.0">
<configuration>
  <system.webServer>
    <httpProtocol>
      <customHeaders>
        <add name="Strict-Transport-Security" value="max-age=16070400; includeSubDomains" />
      </customHeaders>
    </httpProtocol>
  </system.webServer>
</configuration>

這是一個有用的web.config轉換測試器

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM