如何从 web.config 中提取一个部分到一个单独的文件中

Ori*_*er7 5 .net c# vb.net asp.net

我有以下部分Web.config

 <httpProtocol>
  <customHeaders>
    <remove name="X-UA-Compatible" />
    <remove name="X-Frame-Options" />
    <remove name="X-XSS-Protection" />
    <remove name="X-Content-Type-Options" />
    <add name="X-UA-Compatible" value="IE=Edge" />
    <add name="X-Frame-Options" value="DENY" />
    <add name="X-XSS-Protection" value="1; mode=block"></add>
    <add name="X-Content-Type-Options" value="nosniff" />
  </customHeaders>
</httpProtocol>
Run Code Online (Sandbox Code Playgroud)

我想提取<customHeaders>到一个名为 的配置文件web.customer.customHeaders.config。为了实现这一目标,我web.customer.customHeaders.config在与我所在的位置相同的位置创建了该文件,Web.config并在其中编写了以下 XML:

<customHeaders>
    <remove name="X-UA-Compatible" />
    <remove name="X-Frame-Options" />
    <remove name="X-XSS-Protection" />
    <remove name="X-Content-Type-Options" />
    <add name="X-UA-Compatible" value="IE=Edge" />
    <add name="X-Frame-Options" value="DENY" />
    <add name="X-XSS-Protection" value="1; mode=block"></add>
    <add name="X-Content-Type-Options" value="nosniff" />
  </customHeaders>
Run Code Online (Sandbox Code Playgroud)

我还在<customHeaders>我的文件中更改了该部分,Web.config如下所示:

 <httpProtocol>
  <customHeaders configSource="web.customer.customHeaders.config" />
</httpProtocol>
Run Code Online (Sandbox Code Playgroud)

但不幸的是,该configSource属性未被识别。因此,提取的文件无法读取并插入到我的Web.config文件中。

我的问题是:如何从 web.config 中提取一个单独的文件中的部分。

如果您知道如何管理,请在下面发表评论。

pfx*_*pfx 3

并非所有部分都允许configSource属性;customHeaders 就是这样一个。
Visual Studio 使用 XSD 模式来验证例如的内容。web.config证实了这一点。您可以在中找到此文件C:\Program Files (x86)\Microsoft Visual Studio 14.0\Xml\Schemas\1033\DotNetConfig.xsd(除非您安装在其他地方)。

声明的片段customHeaders显示没有configSource属性。

<xs:element name="customHeaders">
  <xs:complexType>
    <xs:choice minOccurs="0" maxOccurs="unbounded">
      <xs:element name="add">
        <!-- ... -->
      </xs:element>
      <xs:element name="remove">
        <!-- ... -->
      </xs:element>
      <xs:element name="clear">
        <!-- ... -->
      </xs:element>
    </xs:choice>
    <xs:anyAttribute />
  </xs:complexType>
</xs:element>
Run Code Online (Sandbox Code Playgroud)

您可以DotNetConfig.xsd找到哪些元素/部分支持此属性;例如。connectionStringsappSettings