Click here to Skip to main content
13,198,465 members (63,235 online)
Click here to Skip to main content
Add your own
alternative version


35 bookmarked
Posted 21 Feb 2011

Split web.config for Different Environments

Rate this:
Please Sign up or sign in to vote.
Splitting web config file for different environments is a brilliant thought, and here I am presenting with complete sample code with design diagrams


Splitting the web.config file for different environments is a brilliant thought, since it has many advantages in the level of reducing work, lack of confusion between different files in different environments, rare chance to mess up files between different environment and also in keeping credentials in a secure place. Microsoft has given the facility to do the same and I think a majority of people are not taking advantage of this facility when they setup different environments.

Advantages of Intelligent web.config Splitting

File Structure for Two Different Environments



UAT and Staging


A Case Study

Here is an old environment:

  1. We have lots of country sites, they have the same source code but separate domains (for some valid reasons) and separate deployment.
  2. We have development, testing, and staging environments.
  3. We do bug fixes, performance optimizations and new enhancements.
  4. We have some contractors in the team and they know almost all credentials.
  5. We have a separate deployment team for testing and staging environments.
  6. We have to raise tickets to correct entry or anything in deployment.

We did in the following way before we thought about splitting web.config:

  1. We use a NAnt script to copy binary files across all country sites.
  2. There is some shared key which has to go to all sites, we do it by opening each country's site and manually updating (sometimes, we are able to do it with a NAnt script, but not always).
  3. We do not care much about database passwords and other credentials.
  4. Sometimes we mess up configuration files in different environments, and we raise another ticket to change it, and wait for that.

Here are the Real Advantages

  1. We store all common keys (appSettings) in a common configuration file and share for all country sites.
  2. All credentials like password are moved to another file which can only be accessed by system administrators.
  3. We maintain separate specific files for different environments, so there is no confusion at all.
  4. Some of the site specific appSettings, we store at site level, and shared appSettings keys are in a shared key file.

You may have many other advantages as well for your scenario.

Source Code Samples

Splitting Keys <appSettings> into two different files.


  <connectionStrings configSource="admin.config" />

    <compilation debug="true" targetFramework="4.0" />
<appSettings file="key.config" >
    <add key="ChildKey1" value="ChildValue1"/>
    <add key="Childkey2" value="ChildValue2"/>

    <modules runAllManagedModulesForAllRequests="true"/>


<connectionStrings >
  <add name="BannerConnectionString" 

connectionString="Data Source=localhost;Initial Catalog=Banner;Integrated Security=True"

providerName="System.Data.SqlClient" />


<?xml version="1.0" encoding="utf-8"?>
<appSettings >
  <add key="ChildKey1" value="ChildValue1"/>
  <add key="ChildKey2" value="ChildValue2"/>

Finally, very important:

How to Protect Split Configuration Files from the End User

It is very important that split files are hidden from the end user, and it can be done through the following two methods:

  1. The administrator can restrict folder level access by keeping new configuration files in a separate folder.
  2. We can restrict files by ASP.NET built-in access system in web.config.
    <location path="Key.Config">
          <deny users="*"/>

Breaking up of web.config does not have much advantage if your environment is any of the following:

  1. Small application with a few developers, and no environment other than development and production.
  2. Your application does not care much about security of databases used in your application.
  3. Your do not have separate administration or deployment teams to do production or UAT deployment.
  4. Configuration files in different environments are always in sync for some reason.

Most People will Ignore This for the Following Reasons

  1. Deployment will happen very rarely, and also it is a one-time job.
  2. People don’t care about security until and unless some unauthorized person accesses the database.
  3. Even if configuration files are messed up in different environments, they can be changed with little effort.


This is a simple idea, but I think it is nice to publicize it, and please let me know if anybody has better ideas, or any comments.

Happy reading!


  • 21st February, 2011: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Justin Mathew @ Investment Bank
Engineer British Standard Institute (BSi), London
United Kingdom United Kingdom
My passion is

Microsoft distributed and connecting Technologies like Windows Communication foundation (WCF)
Workflow Services (WWF)
SQL Server
Optimization in all of these

and more @

You may also be interested in...


Comments and Discussions

SuggestionSeveral Recommendations Pin
MacSpudster13-Nov-13 7:23
memberMacSpudster13-Nov-13 7:23 
SuggestionI have implemented web.config separation via location attribute. Pin
urpalsush15-Mar-13 11:34
memberurpalsush15-Mar-13 11:34 
GeneralMy vote of 5 Pin
bouncex@gmail.com20-Jul-12 0:51
memberbouncex@gmail.com20-Jul-12 0:51 
QuestionWhy? Pin
Johnny J.18-Oct-11 20:37
memberJohnny J.18-Oct-11 20:37 
AnswerRe: Why? Pin
MacSpudster13-Nov-13 7:09
memberMacSpudster13-Nov-13 7:09 
GeneralRe: Why? Pin
Johnny J.13-Nov-13 22:13
professionalJohnny J.13-Nov-13 22:13 
QuestionOne thing about IIS & .config files Pin
jtrz23-Jun-11 10:34
memberjtrz23-Jun-11 10:34 
AnswerRe: One thing about IIS & .config files Pin
Canny Brisk1-Sep-13 20:50
professionalCanny Brisk1-Sep-13 20:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171020.1 | Last Updated 21 Feb 2011
Article Copyright 2011 by Justin Mathew @ Investment Bank
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid