如何在单独的文件中从web.config中提取节

时间:2021-02-27 20:20:38

I have the following section in Web.config :

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

and I would like to extract <customHeaders> to a config file with the name web.customer.customHeaders.config. In order to achieve this, I have created the web.customer.customHeaders.config file in tha same location where my Web.config is and I have written the folowing XML in it:

我想将 解压缩到名为web.customer.customHeaders.config的配置文件中。为了实现这一点,我在我的Web.config所在的同一位置创建了web.customer.customHeaders.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>

I have also chsnged the <customHeaders> section in my Web.config file as such:

我还在我的Web.config文件中修改了 部分:

 <httpProtocol>
  <customHeaders configSource="web.customer.customHeaders.config" />
</httpProtocol>

but unfortunately, the configSource attribute is not recognised. As a result, the extracted file, cannot be read and be inserted into my Web.config file.

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

My question is: How can I extract a section from web.config in a seperate file.

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

In case you have any clue how this managable is, please leave a comment below.

如果您对此可管理性有任何疑问,请在下面留言。

1 个解决方案

#1


0  

Not all sections allow a configSource attribute; customHeaders is such one.
The XSD schema Visual Studio uses to validate the content of eg. web.config confirms this. You find this file in C:\Program Files (x86)\Microsoft Visual Studio 14.0\Xml\Schemas\1033\DotNetConfig.xsd (unless you installed elsewhere).

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

A fragment of the customHeaders declaration shows there is no configSource attribute.

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>

In DotNetConfig.xsd you find which elements/sections do support this attribute; eg. connectionStrings and appSettings.

在DotNetConfig.xsd中,您可以找到哪些元素/部分支持此属性;例如。 connectionStrings和appSettings。

#1


0  

Not all sections allow a configSource attribute; customHeaders is such one.
The XSD schema Visual Studio uses to validate the content of eg. web.config confirms this. You find this file in C:\Program Files (x86)\Microsoft Visual Studio 14.0\Xml\Schemas\1033\DotNetConfig.xsd (unless you installed elsewhere).

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

A fragment of the customHeaders declaration shows there is no configSource attribute.

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>

In DotNetConfig.xsd you find which elements/sections do support this attribute; eg. connectionStrings and appSettings.

在DotNetConfig.xsd中,您可以找到哪些元素/部分支持此属性;例如。 connectionStrings和appSettings。