Click here to Skip to main content
Click here to Skip to main content

Tagged as

Supporting Development and Release web.config in ASP.NET

, 7 Jan 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
Supporting Development and Release web.config in ASP.NET
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.
 
Web.config has two sections we are interested in:
1) AppSettings
2) ConnectionSettings
 
Let's put these in two separate files: WebAppSettings.config and WebConnectionSettings.config.
 
More files to maintain! No, not really. With AppSettings, 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 appSettings section contains a reference to the new file:
<appSettings file="WebAppSettings.config">
...
</appSettings>
You can keep all your normal app settings for production:
<appSettings file="WebAppSettings.config">
  <add key="Development" value="False"/>
  <add key="SMTPServer" value="smtp.mycompany.com"/>
</appSettings>
 
Your WebAppSettings.config file contains only the settings for development:
<appSettings>
  <add key="Development" value="True"/>
  <add key="SMTPServer" value="smtp.mydevelopmentserver.net"/>
</appSettings> 
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 connectionStrings 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 "configSource" attribute of the "connectionStrings" tag in your web.config (remember the section must now be empty):
 
<connectionStrings configSource="WebConnectionSettings.config">
</connectionStrings>
 
The "WebConnectionSettings.config" file contains only the connections strings section:
 
<connectionStrings>
    <clear/>
    <add name="MySqlServer"
        connectionString="Data Source=(local);Initial Catalog=MyProductionDB;Integrated Security=True"
        providerName="System.Data.SqlClient" />
</connectionStrings>
Keep a second version for development only, and you are good to go.

License

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

Share

About the Author

OriginalGriff
CEO
Wales Wales
Born at an early age, he grew older. At the same time, his hair grew longer, and was tied up behind his head.
Has problems spelling the word "the".
Invented the portable cat-flap.
Currently, has not died yet. Or has he?
Follow on   Google+

Comments and Discussions

 
GeneralReason for my vote of 5 Good one Pinmvpthatraja7-Jan-11 14:56 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.141015.1 | Last Updated 7 Jan 2011
Article Copyright 2011 by OriginalGriff
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid