Techbubbles

Working with Configuration files

Introduction

Configuration files are central place to configure your web or windows applications. The web.config file is used for configuring the web applications and app.config for windows applications.

The ASP.NET Application comes with web.config file with default configuration sections and you can easily manage them at the intialstage of the project development. As soon as your project grows mature, web.config file turns out to be a huge file.

The configuration files can be different from development environment to production environment.

Example: Connection strings, Application settings, logging and compilation settings are differed from development to production environment.

Maintaining multiple configuration files is pain to manage, For this .NET configuration system has a feature that each configuration section can define an attribute named “configSource”  to define an alternative location from where the configuration file has to be loaded.

Example: In web.config file locate the connectionStrings section and add a new attribute name configSource and give the value as “MyConnectionStrings.config”.

   1: <connectionStrings configSource="MyConnectionStrings.config"/>

Add a new file called “MyConnectionStrings.config” to the same directory where web.config lives and define its contents as follows:

   1: <?xml version="1.0"?>

   2: <connectionstrings>

   3:     <add name="MyDataBase" connectionstring=” ”/> 

   4: <connectionstrings>

Key points:

  • External configuration file must contain the section name as the root element.
  • If you use configSource you must not define any other attribute or child element for the respective section in the web.config file.
  • It is useful if you move all your configuration files to subfolder to keep root directory clean.    

Related Posts:

%d bloggers like this: