简体   繁体   English

Web.config转换添加树

[英]Web.config transforms add tree

I want to add the following to the web config on release: 我想在发布时将以下内容添加到Web配置中:

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

There aren't any custom headers in the default web config so I get an error when I publish: No element in the source document matches '/configuration/system.webServer/httpProtocol/customHeaders' . 默认的Web配置中没有任何自定义标头,因此发布时会出现错误: No element in the source document matches '/configuration/system.webServer/httpProtocol/customHeaders'

I can fix it my just adding the empty elements to the web.config like so: 我可以修复它,只需将空元素添加到web.config中,如下所示:

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

However, it doesn't feel like the correct way. 但是,这感觉不正确

Is there a more correct way to build the element tree on the transform? 有没有更正确的方法在转换上构建元素树?

Adding the empty <customHeaders> node to the web.config works because the transform you have is to insert the <add .../> node, not the <customHeaders> node. 将空的<customHeaders>节点添加到web.config中是<customHeaders> ,因为您进行的转换是插入<add .../>节点,而不是<customHeaders>节点。 It can only insert where it matches to that point. 它只能插入与该点匹配的位置。

To insert the tree of nodes, move the xdt:Transform="Insert" up a little in the XML. 要插入节点树,请在XML中向上移动xdt:Transform="Insert" If you start with a web.config of: 如果从以下Web.config开始:

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

and transform it with: 并将其转换为:

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

You'll end up with: 您最终将得到:

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

Here's a helpful web.config transformation tester . 这是一个有用的web.config转换测试器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM