Web.Config: Multiple file configurations

ASP.NET Web.Config files provide a configuration system we can use to keep our applications flexible at runtime. In this article we will examine a simple technique for using the configuration system for the best results.

The < appSettings > element of a web.config file is a place to store connection strings, server names, file paths, and other miscellaneous settings needed by an application to perform work. The items inside appSettings are items that need to be configurable depending upon the environment, for instance, any database connection strings that will change as our applications are moved from a testing/staging server into a production environment.

Developers often find themselves having to take extra care not to overwrite web.config file as they move code from one environment to the next, typically from develeopment, to staging/testing, and finally to production. Let's take a look at a little known feature of the appSettings element that can give us even more flexibility (see source code in url below).

The appSettings element may contain a file attribute that points to an external file.If the external file is present, ASP.NET will combine the appSettings values from web.config with those in the external file. If a key/value pair is present in both files, ASP.NET will use the value from the external file.

This feature is useful when you keep user-specific or environment-specific settings in the external file. Let web.config contain those settings that are global to all the installed instances of your application, while each user or installed site contains their own settings in an external file. This approach makes it easier to move around global web.config changes and keep web.config checked into source control, while each developer can get their own settings separate.

One caveat to this approach is that the ASP.NET runtime does not detect when the external file changes. You’ll need to make changes to web.config itself for ASP.NET to launch a new version of the application with all changes in effect.

In the attached code snippet (see link below), lines 2 through 8 shows a typical implemention of a web.config file that points to an external configuration file. Lines 13 through 15 shows what the external file might look like.

http://www.solid2.com/jaycent/articles/code_display.aspx?codeid=adedb5b5-ca86-4dbe-9c5e-1a16dd45b292

3 Comments

Comments have been disabled for this content.