views:

51

answers:

3

Is there a way to have the main .net configuration file app.config/web.config include another configuration file? I have a need to keep things in separate files, but link them together.

Here is a sample of what I want to include:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
      <section name="LocationSearch.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    </sectionGroup>
  </configSections>
  <applicationSettings>
    <LocationSearch.Properties.Settings>
        <setting name="MapQuestApiKey" serializeAs="String">
        <value>some value here...</value>
        </setting>
        <setting name="MapQuestClientCode" serializeAs="String">
        <value>another value here...</value>
        </setting>
    </LocationSearch.Properties.Settings>
  </applicationSettings>
</configuration>
+3  A: 

Yep, you can include separate files into the main .config using the configSource attribute like so:

<securityConfiguration configSource="SomeOtherConfigFile.config" />

External config files that you include this way must be within the same directory (or a subfolder) of the main .config file.

SectionInformation.ConfigSource Property

Kevin Tighe
@Kevin Nice! Any specific reason the element is called "securityConfiguration" ?? I guess I would expect something like "<include configSource..."
Paul Fryer
You can use the configSource attribute with any configuration section I believe. I just used securityConfiguration as an example because it was in a config file I had handy :)
Kevin Tighe
Sounds like you can just add the "configSource" attribute to any element and it will inject whatever is in the linked file, correct?
Paul Fryer
I believe you can only use the configSource attribute with a ConfigurationSection, like appSettings, securityConfiguration, etc.
Kevin Tighe
+1  A: 

Yes, I do this for URL rewriting rules.

So in my web.config I do this:

<urlrewritingnet configSource="UrlRewriting.config" />
Dustin Laine
+2  A: 

Yes, with the 'file' attribute

<appSettings file="configFiles/otherConfigFile.config">

MSDN info

Jim Ecker