Working with more then one web.config files in asp.net application.

Recently one of reader of my blog how we can work with more then one web.config files in asp.net application. So I decided to blog about that. Here is the my reply for that.

You can work with more then one web.config file in asp.net. But you can not put more then one web.config in each folder. Let’s first understand the hierarchy of web.config and other configuration file settings. On the top of the every configuration files you will have machine.config file which will have all system wide configuration settings.You can find this file in your OS drive like C: /windows/Microsoft.NET/vFrameworkNumber/Config folder. Here framework number with what ever framework you are using 1.1/2.0 or 4.0. You can override those settings in web.config file at the your application root folder. Same way you can add more web.config file in subfolder and can override the setting of parent folder web.config file. So we will hierarchy like below.

Hirerchay

Now let’s Create Project for it. In that I have create two web.config and 2 pages. First I have putted the web.config in root folder and then I have putted web.config in subfolder. Same way I have created a sub folder and then I have putted the web.config in sub folder. I have also putted one asp.net page in root as well as subfolder to use respective web.config settings. Here are my folder structure like below.

FolderStructure

Below is code for root folder web.config

<?xml version="1.0"?>
<configuration>
    <system.web>
        <compilation debug="true" targetFramework="4.0" />
    </system.web>
  <appSettings>
    <add key="root" value="This is from root web.config"></add>
    <add key="MySetting" value="This my settings is from root web.config"></add>
  </appSettings>

</configuration>

and following is code for sub folder web.config.

<?xml version="1.0"?>
<configuration>
    <system.web>
    </system.web>
  <appSettings>
    <add key="sub" value="This is from sub web.config settings"></add>
    <add key="MySetting" value="This my settings is from sub folder web.config"></add>
  </appSettings>
</configuration>

After that I have written a code in root asp.net page to print settings from web.config folder like this following.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MoreWebConfig
{
    public partial class Root : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Write(System.Web.Configuration.WebConfigurationManager.AppSettings.Get("Root"));
            Response.Write("<BR>");
            Response.Write(System.Web.Configuration.WebConfigurationManager.AppSettings.Get("MySetting"));

        }
    }
}

Same way I have wrriten code in subfolder like following.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MoreWebConfig.SubFolder
{
    public partial class SubFolderPage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Write(System.Web.Configuration.WebConfigurationManager.AppSettings.Get("Sub"));
            Response.Write("<BR>");
            Response.Write(System.Web.Configuration.WebConfigurationManager.AppSettings.Get("MySetting"));

        }
    }
}

Now let’s run the both pages in browser one by one and you can see root folder application page is fetching settings from root folder web.config while sub folder application page is fetching setting from subfolder web.config even if key ‘mysetting’ is same on both as expected. You can see out put in browser below.

Root.aspx

Root

SubFolderPage.aspx

SubFolder

So it’s very easy to work with multiple web.config. The only limitation of this you can not access sub folder web.config settings from root folder page. Except all you can use anything. Hope you liked it. Stay tuned for more..Happy programming..

Technorati Tags: ,
Shout it
Published Wednesday, March 23, 2011 7:08 PM by Jalpesh P. Vadgama

Comments

# re: Working with more then one web.config files in asp.net application.

Friday, March 25, 2011 3:15 PM by Bruno

And when using MasterPages? Will the same code in the root folder MasterPage use the root or the sub web.Config?

What about UserControls? If used in an UserControl, and the UserControl is used in a root folder page, what will happen than?

# re: Working with more then one web.config files in asp.net application.

Friday, March 25, 2011 3:30 PM by Jalpesh P. Vadgama

If master page is on root folder then it will use root folder and if in sub folder then sub folder. Same way it applies for user control.

# re: Working with more then one web.config files in asp.net application.

Sunday, March 27, 2011 4:24 AM by sharepointecommerce

We see the need in SharePoint as well

# re: Working with more then one web.config files in asp.net application.

Sunday, March 27, 2011 1:36 PM by hajan

Nice, very nice! ;)

# re: Working with more then one web.config files in asp.net application.

Sunday, March 27, 2011 8:00 PM by Nick Samson

This article is so elementary. Why bother posting it.

# re: Working with more then one web.config files in asp.net application.

Monday, March 28, 2011 7:08 AM by ahsanm.m

thanks for sharing it.

# re: Working with more then one web.config files in asp.net application.

Monday, March 28, 2011 8:13 AM by ziaur10

thanks! for your discovering. My understanding is that web config would work like variables. Like same variable name both for local and global then  this sort of reflection would be appeared.

If I am wrong let me know.

# re: Working with more then one web.config files in asp.net application.

Monday, March 28, 2011 1:54 PM by John Goode

While elementary Nick, it is useful info to some. Nice job Jalpesh.

# re: Working with more then one web.config files in asp.net application.

Monday, March 28, 2011 11:09 PM by Rai

This one is the basic concept.Why this one is posted on main asp.net site. Guys you should post some thing new please.............

# re: Working with more then one web.config files in asp.net application.

Tuesday, March 29, 2011 3:05 AM by Rashmi Agarwal

I have been using this approach for quite sometime for different permission settings. Howerver, I could not find the way to use different web.config files for debug and release modes.

It will be great if someone can help me on this.

# re: Working with more then one web.config files in asp.net application.

Tuesday, March 29, 2011 10:14 AM by Jalpesh P. Vadgama

@Rai- But still lots of people does not this concept which was there since 1.1 and but still they are struggling with that and I tried to give attention to this topic.

# re: Working with more then one web.config files in asp.net application.

Tuesday, March 29, 2011 9:20 PM by Manivannan

Hi,

The article is very usefull and very easy to understand about this concept.

# re: Working with more then one web.config files in asp.net application.

Wednesday, March 30, 2011 8:33 AM by Charles Omordia

Thanks. I was able to resolved my web.config problem

# re: Working with more then one web.config files in asp.net application.

Wednesday, March 30, 2011 9:21 AM by Burzyn

It would be worth to mention that certain settings can only be used in root web.config.

Very elementary indeed. What's the target audience?

# re: Working with more then one web.config files in asp.net application.

Wednesday, March 30, 2011 10:07 AM by mtcholakian

Very nice sample!

# re: Working with more then one web.config files in asp.net application.

Thursday, March 31, 2011 11:36 AM by The article is so elementary!

It would be worth to mention that certain settings can only be used in root web.config.Very elementary indeed. What's the target audience?

# re: Working with more then one web.config files in asp.net application.

Monday, June 27, 2011 2:02 AM by deepali

very nice and easy article...thanks alot...

# re: Working with more then one web.config files in asp.net application.

Friday, August 26, 2011 12:42 AM by Manjith

Very nice.  It will help to understand the multiple web.confg for freshers.

Leave a Comment

(required) 
(required) 
(optional)
(required)