2015-10-04 93 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