5

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.

Origamer7
  • 315
  • 3
  • 17
  • `the configSource attribute is not recognised.` Is the IDE indicating that somehow? Or you are experiencing it at runtime? – mjwills May 30 '18 at 13:06
  • @mjwills : Yes. There is a squiggly line and when I go with my mouse over, I become the hint "The 'configSource' is not allowed." – Origamer7 May 30 '18 at 13:09
  • And when you run it does it work? See https://stackoverflow.com/a/5980776/34092 – mjwills May 30 '18 at 13:09
  • This might help. https://stackoverflow.com/questions/2232059/reading-settings-from-separate-config-file – Misam May 30 '18 at 13:11
  • When you say extract I thought it means extracting through code. This works for console app.config. Should not be different with web.config `Configuration configuration = ConfigurationManager.OpenExeConfiguration(Assembly.GetEntryAssembly().Location); var section = configuration.GetSection("runtime"); File.WriteAllText(@"C:\Temp\section.config", section.SectionInformation.GetRawXml());` – Nilesh May 30 '18 at 13:12
  • @mjwills: The code compiles, but I get an error "500.19 - Internal Server Error: The requested page can not be accessed because its configuration information for the page is invalid." whe I try to load the Application. – Origamer7 May 30 '18 at 13:15
  • @mjwills: I am afraid I am not allowed to upload the whole configuration. I can assure you that the application runs properly if I take my changes back. – Origamer7 May 30 '18 at 13:29
  • 1
    Does the configSource file have an xml Element header indicated as required in this [answer](https://stackoverflow.com/a/398632/2592875)? – TnTinMn May 30 '18 at 14:50
  • @mjwills: It will not make any difference if I just added the XML header to my seperate file, because the problem is that the "configSource" attribute is not recognised as an attribute for the "customHeader" tag. – Origamer7 Jun 05 '18 at 14:08
  • @Nilesh: I got a little bit confused. I think I need only some XML code. – Origamer7 Jun 05 '18 at 14:13

1 Answers1

3

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.

pfx
  • 20,323
  • 43
  • 37
  • 57
  • Looked at the XSD file and now I'm learning the location section/element is another section that does not allow configSource attribute; damn!. I wanted to place location elements in a separate file but will have to live with a larger web.config. Thanks pfx! – Only You Mar 04 '19 at 16:45