Ban*_*San 2 .net asp.net-mvc web-config webmatrix web.config-transform
我想在发布时将以下内容添加到 web 配置中:
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Strict-Transport-Security" value="max-age=16070400; includeSubDomains" xdt:Transform="Insert" />
</customHeaders>
</httpProtocol>
</system.webServer>
Run Code Online (Sandbox Code Playgroud)
默认 Web 配置中没有任何自定义标头,因此在发布时出现错误: No element in the source document matches '/configuration/system.webServer/httpProtocol/customHeaders'.
我可以修复它,只需将空元素添加到 web.config 中,如下所示:
<httpProtocol>
<customHeaders>
</customHeaders>
</httpProtocol>
Run Code Online (Sandbox Code Playgroud)
但是,感觉这不是正确的方法。
有没有更正确的方法在变换上构建元素树?
将空<customHeaders>节点添加到 web.config 有效,因为您拥有的转换是插入<add .../>节点,而不是<customHeaders>节点。它只能插入与该点匹配的位置。
要插入节点树,请xdt:Transform="Insert"在 XML 中向上移动一点。如果您从 web.config 开始:
<?xml version="1.0">
<configuration>
<system.webServer>
<httpProtocol />
</system.webServer>
</configuration>
Run Code Online (Sandbox Code Playgroud)
并将其转换为:
<?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>
Run Code Online (Sandbox Code Playgroud)
你最终会得到:
<?xml version="1.0">
<configuration>
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Strict-Transport-Security" value="max-age=16070400; includeSubDomains" />
</customHeaders>
</httpProtocol>
</system.webServer>
</configuration>
Run Code Online (Sandbox Code Playgroud)
这是一个有用的web.config 转换测试器。