When you are developing or maintaining a website, it is normal to keep separate databases, app settings, etc. to prevent corruption of live data.
The problem is, you really don't want to maintain separate versions of any files, and that includes "web.config
The trouble is, web.config
contains so much important stuff, that it is a real pain to maintain separate versions for development and production - it just leaves too much scope for mistakes, which can get expensive.
Fear not! You can easily maintain a single "web.config
", but keep development settings and configuration strings out of this file, but available when you need them.
How? By maintaining four files instead. No, no, keep on reading, it's not as mad as it sounds.
has two sections we are interested in:
Let's put these in two separate files: WebAppSettings.config
More files to maintain! No, not really. With
, we can provide default values in Web.Config
which are overridden if and only if WebAppSettings.config
is present. So, the defaults are for production, the development ones go in WebAppSettings.config
which is kept back from source control and never exported to the production environment. Result? Oh yes! How do we do it?
Change your web.config
so that the
section contains a reference to the new file:
You can keep all your normal app settings for production:
<add key="Development" value="False"/>
<add key="SMTPServer" value="smtp.mycompany.com"/>
file contains only the settings for development:
<add key="Development" value="True"/>
<add key="SMTPServer" value="smtp.mydevelopmentserver.net"/>
Done! If the file is there, it works with development settings. If not, it works with production.
We can do a similar thing with connection strings, but unfortunately without the defaults - the
section must be empty if you use an external file. Still, it is simpler to maintain a production and a development WebConnectionSettings.config
file that only ever change when the server changes than to maintain two separate but parallel web.config
files which may change quite frequently.
Use the "
" attribute of the "
" tag in your web.config
(remember the section must now be empty):
" file contains only the connections strings section:
connectionString="Data Source=(local);Initial Catalog=MyProductionDB;Integrated Security=True"
Keep a second version for development only, and you are good to go.