Click here to Skip to main content
15,881,139 members
Articles / Web Development / ASP.NET
Article

Working with more than one Web.config file

Rate me:
Please Sign up or sign in to vote.
4.19/5 (24 votes)
18 Jul 20052 min read 245.6K   2.7K   53   16
We can have more than one <i>Web.config</i> file in ASP.NET applications, by adding separate <i>Web.config</i> files for subfolders.

Introduction

I would like to share what I have understood about working with more than one Web.config file from my latest ASP.NET application. We planned to have different Web.config files for sub-folders in the application root folder. It helps us to have small and easily maintainable configuration files.

Hierarchy of Web.config Files

System wide configuration settings are defined in the Machine.config for the .NET Framework. The Machine.config file is located in the C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\CONFIG folder. The settings defined in this file are applicable to all ASP.NET applications in that system.

We can override these default settings by including a Web.config file in the application's root folder.

By including Web.config files in sub-folders, we can override the settings defined in the Web.config file in the application's root folder.

The following are sample section declarations from a Machine.config file:

XML
<section name="processModel" 
  type="System.Web.Configuration.ProcessModelConfigurationHandler, 
        System.Web, Version=1.0.5000.0, Culture=neutral, 
        PublicKeyToken=b03f5f7f11d50a3a" 
  allowDefinition="MachineOnly"/>
 
<section name="sessionState" 
  type="System.Web.SessionState.SessionStateSectionHandler, 
        System.Web, Version=1.0.5000.0, Culture=neutral, 
        PublicKeyToken=b03f5f7f11d50a3a" 
  allowDefinition="MachineToApplication"/>
 
<section name="appSettings" 
  type="System.Configuration.NameValueFileSectionHandler, System,
        Version=1.0.5000.0, Culture=neutral, 
        PublicKeyToken=b77a5c561934e089"/>

There is an attribute allowDefinition specified in the first two section declarations with the values: MachineOnly and MachineToApplication.

What it does mean?

If allowDefinition="MachineOnly", then we can not override this section either in application level or in folder level. The only section declared in the Machine.config file with this settings is processModel.

If allowDefinition="MachineToApplication", then we can override these sections by the root directory Web.config. Sections with this setting in Machine.config are authentication, machineKey, sessionState, trust, and securityPolicy.

If allowDefinition attribute is omitted in a section declaration of the Machine.config file, we can override that section at any level.

We can override the section appSettings at any level and can access it by using ConfigurationSettings.AppSettings easily.

What is there in the sample project?

The sample source code is a simple ASP.NET web application with three Web Forms and three Web.config files. The root folder has a sub folder SubFolder_L1 that has SubFolder_L2, each has one Web Form and one Web.config file.

Web.config files have different and overridden keys. Web.config file in the root folder has the following appSettings declarations:

XML
<appSettings>
    <add key="root" value="Root folder's configuration file."/>
    <add key="color" value="Blue"/>
</appSettings>

Web.config file in Subfolder_L1 has the following appSettings declarations:

XML
<appSettings>
    <add key="subfolder_l1" value="Subfolder_L1\web.config file."/>
    <add key="color" value="Green"/>
</appSettings>

The color setting is overridden by the subfolder configuration file. We can read the root element from Subfolder_L1 or Subfolder_L2 by the following code:

C#
lblConfig.Text = ConfigurationSettings.AppSettings["root"];

But we can not read configuration settings defined in Subfolder_L1's Web.config file from the root folder.

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


Written By
Architect
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Generalneed explanation Pin
hitech_s25-Feb-11 23:49
hitech_s25-Feb-11 23:49 
Generallogin.aspx Pin
Ajay Kale New27-Sep-10 0:12
Ajay Kale New27-Sep-10 0:12 
Generalautomatic redirection to Login.aspx Pin
Ajay Kale New10-Sep-10 4:21
Ajay Kale New10-Sep-10 4:21 
GeneralVery handy Information for developers Pin
gogsthecoder15-May-09 18:25
gogsthecoder15-May-09 18:25 
GeneralReg:- Working with more than one Web.config file Pin
SRKVELLANKI9-Aug-08 17:16
SRKVELLANKI9-Aug-08 17:16 
Smile | :) Hi Palanisamy Veerasingam,

The article you posted is excellent.Good job Buddy.I learnt a lot reading it,if u have any such articles in .NET pls post to srkvellanki@yahoo.co.in

Regards
SriRam
GeneralReading Multiple SubFodler Config Files Pin
KothariGaurav18-Jun-08 3:22
KothariGaurav18-Jun-08 3:22 
Generalmultiple projects using single web.config Pin
sheth.mital5-Sep-07 19:14
sheth.mital5-Sep-07 19:14 
GeneralRe: multiple projects using single web.config Pin
Palanisamy Veerasingam7-Sep-07 4:02
Palanisamy Veerasingam7-Sep-07 4:02 
GeneralHard to understand English Pin
kb10130-May-07 11:47
kb10130-May-07 11:47 
GeneralRe: Hard to understand English Pin
Palanisamy Veerasingam31-May-07 1:00
Palanisamy Veerasingam31-May-07 1:00 
GeneralRe: Hard to understand English Pin
jKiumo11-Jun-07 23:01
jKiumo11-Jun-07 23:01 
QuestionAucune réponse?? Pin
momo00719-Jul-06 5:31
momo00719-Jul-06 5:31 
GeneralThanks! Pin
momo00719-Jul-06 1:30
momo00719-Jul-06 1:30 
GeneralRe: Thanks! Pin
Palanisamy Veerasingam20-Jul-06 19:06
Palanisamy Veerasingam20-Jul-06 19:06 
GeneralRe: Thanks! Pin
momo00721-Jul-06 1:01
momo00721-Jul-06 1:01 
GeneralRe: Thanks! Pin
Palanisamy Veerasingam23-Jul-06 19:26
Palanisamy Veerasingam23-Jul-06 19:26 

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

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