How to Include Multiple .Config Files in ASP.NET Web Application

Include Multiple .Config Files in ASP.NET Web Application

Introduction

Microsoft ASP.Net provides a configuration system that can be used to keep our applications flexible at run-time. In this article, I will focus on using multiple configuration files. This is a rare technique and is unknown to many developers. The usage of multiple configuration file makes the application more secure and manageable.

Prerequisites

This Postl assumes that you own a copy of Visual Studio 2005 or Visual Web Developer Express. It also assumes that you are familiar with ASP.Net 2.0 basics and have worked with web.config before.

Working with Web.Config

Web.config exposes an <appSettings> element that can be used as a place to store application settings like connection strings, file paths, etc. Using the web.config is an ideal method of creating a robust application that can quickly adapt to changes in its environment. For instance, if the connection string is stored in web.config and is being called from the web-pages from there, then changes in the connection string will have to be made in web.config only. Otherwise, the user would have to go to each page individually and update the connection string.
Let us look at a basic web.config which holds our connection string.

<?xml version=”1.0″?>
<configuration>
<appSettings/>
<connectionStrings/>
<system.web>
<compilation debug=”false” strict=”false” explicit=”true” />
</system.web>
<appSettings>
<add key=”myConnInfo” value=”server=_;database=_;user=_;pass=_;” />
</appSettings>
</configuration>

To read the connection setting from the Config file, you have to use a single line of code:

System.Configuration.ConfigurationManager.AppSettings(“ConnectionInfo”)

Multiple Config Files

The appSettings element can contain a file attribute that points to an external file. I will change my web.config file to look like the following:
<?xml version=”1.0″?>
<configuration>
<appSettings/>
<connectionStrings/>
<system.web>
<compilation debug=”false” strict=”false” explicit=”true” />
</system.web>
<appSettings file=”externalSettings.config”/>
</configuration>

Next, we can create the external file “externalSettings.config” and add an appSettings section with our connection information and any other settings that we want to use.
If the external file is present, ASP.Net combines the appSettings values from web.config with those in the external file. If a key/value exists in both files, then ASP.Net will use the setting from the external file.
This feature is useful when one keeps user-specific or environment-specific settings in the external file. It is better to design web.config to contain those settings that are global, while each user setting is contained in an external file. This approach makes it easier to move around global web.config changes.
One caution to this approach is that ASP.Net runtime does not detect when the external file changes. Thus to launch a new version of the application with all changes in effect, one will need to make changes to the web.config itself.

Any comments and reviews are welcome:

Reference : Dilip Kumar Jena ( https://mstechexplore.wordpress.com )

Advertisements

4 thoughts on “How to Include Multiple .Config Files in ASP.NET Web Application

    • We basically go for multiple web.config files in situations where we have multiple Sub-sites and their configuration is different, i.e, having multiple databases then we can continue to use the same names without bothering about the config and database, and when it comes to execution the inner most web.config file take the precedence and executes. That is the main reason for having Multiple config file.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s