2015-10-04 46 views
2

我想補充以下內容上發佈web配置:Web.config文件轉換添加樹

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

但是,它不覺得像正確方式。

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

+0

已嘗試刪除xmlns =屬性? –

回答

3

將空的<customHeaders>節點添加到web.config可行,因爲您擁有的變換是插入<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 transformation tester