How to split the web.config into mutliple files

If you’ve ever experienced any pain from having to merge your web.config changes with the web.config in the live environment, or accidentially overwritten the live web.config file this may be a solution for you. You can specify that certain sections of your web.config reside in a seperate file. So you could have a ConnectionStrings.config file containing your connection strings (for the specific environment) and a web.config file that is common across all environments.

To do this you simply add the attribute configSource=”ConnectionStrings.config” to the XML node you want to replace with a file, and then in the file you’ve referenced you simply place the XML that would normally have been in your web.config file. This will allow you do deploy the web.config to different environments without overriding environment specific settings.

Example, in the web.config:

<connectionStrings configSource="ConnectionStrings.config" />

Then in ConnectionStrings.config:

    <add name="ConnectionString1" connectionString="server=localhost; Integrated Security=True; Database=Database1;" providerName="System.Data.SqlClient" />
    <add name="ConnectionString2" connectionString="server=localhost; Integrated Security=True; Database=Database2;" providerName="System.Data.SqlClient" />

Easy as that 🙂

One thought on “How to split the web.config into mutliple files

Comments are closed.