How to use the configSource.

By | March 25, 2018

The size of configuration files often creates the desire to split it into multiple smaller more manageable parts. To achieve this one can break the file into smaller more manageable parts by using configSource attribute.

 

How to use the configSource attribute?

Below I will show how to to split the appsettings from the web.config into a separate file.

  1. I created a sub folder for my configuration files under App_Data and created a config file called Configuration.Appsettings.config.
  2. Copy the appsettings section to the new file and add the xml deceleration must be added to the first line of the new file;
    <?xml version="1.0" standalone="yes"?>
    

    The complete file should look something like this.

    <?xml version="1.0" standalone="yes"?>
    <appSettings>
    <add key="Product" value="STS" />
    <add key="TokenLifeTimeHours" value="8" />
    <add key="MaxTokenLifeTimeHours" value="10" />
    <add key="ResetLinkLifeTimeHours" value="24" />
    </appSettings>
  3. Return to the main config file and alter the original appsettings section. Use the configSource to instruct the config where the new file can be found.
    <appSettings configSource="App_Data\Configuration\Configuration.Appsettings.config"/>
    

Something that must be understood is that the attribute can only be found on sections and not on section groups, this does limit its ability somewhat.

See more Examples here relating to connectionstrings, system.servicemodel and authorization here.

Leave a Reply

Your email address will not be published.