简体   繁体   中英

How to extract a section from web.config in a seperate file

I have the following section in 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:

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

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

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

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

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).

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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