Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Supporting Development and Release web.config in ASP.NET

0.00/5 (No votes)
7 Jan 2011 1  
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:
XML
<appSettings file="WebAppSettings.config">
...
</appSettings>

You can keep all your normal app settings for production:
XML
<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:
XML
<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):

XML
<connectionStrings configSource="WebConnectionSettings.config">
</connectionStrings>


The "WebConnectionSettings.config" file contains only the connections strings section:

XML
<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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here