Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

how to use multiple web.config files in asp.net application or working with more than one web.config file in asp.net application

Apr 18, 2011

Introduction:

Here I will explain how to use multiple web.config files in single application using asp.net

Description:

I attended one interview at that time they asked one question that is it possible to use multiple web.config files in single asp.net application? For that question answer is Yes we can use multiple web.config files in single web application but how? By creating web.config file in sub root folders we can use multiple web.config files in our application but each folder should contains only one web.config file. Here I will explain step by step how we can use multiple web.config files in our application.

First create one new web application that contains Default.aspx page and one web.config file. Now right click on your application and select New folder after creation of new folder right click on new folder select Add new item and add one aspx page and one web.config file to this folder that files structure like this

After that write the following code in root folder web.config file appsettings section like this
<appSettings>
<add key="rootfolder" value="Root Folder web.config"/>
<add key="test1" value="Testing the application for root folder web.config"/>
</appSettings >
After that open Default.aspx page in root folder and write the following code

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>RootFolder web.config</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<b>
Root Folder values
</b>
</div>
<br />
<div>
AppKey value:
<b>
<asp:Label ID="lbltxt" runat="server" Text="<%$appSettings:rootfolder %>"/>
</b>
<br />
Appkey Test value:
<b>
<asp:Label ID="Label1" runat="server" Text="<%$appSettings:test1 %>"/>
</b>
</div>
</form>
</body>
</html>
Now run your application and check the output for root folder
Demo

After that open the sub root folder web.config file and write the following code in appsettings section like this
<appSettings>
<add key="subrootfolder" value="Sub Root Folder web.config"/>
<add key="test1" value="Testing the application for sub root folder web.config"/>
</appSettings >
After that open ChildPage.aspx page in sub root folder and write the following code

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>SubRoot Folder web.config</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<b>
SubRoot Folder values
</b>
</div>
<br />
<div>
AppKey value:
<b>
<asp:Label ID="lbltxt" runat="server" Text="<%$appSettings:subrootfolder %>"/>
</b>
<br />
Appkey Test value:
<b>
<asp:Label ID="Label1" runat="server" Text="<%$appSettings:test1 %>"/>
</b>
</div>
</form>
</body>
</html>
Now run your application and check the output for sub root folder
Demo

If you observe above outputs Default.aspx page getting values from root folder web.config file and ChildPage.aspx page getting values from subroot folder web.config file.

In this way we can use multiple web.config files in asp.net web application. I hope it helps you

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

20 comments :

Anonymous said...

teri maa di lun saleya bunfd mraaa fude ch vadh k .....aide utte charh.....

Anonymous said...

this is helpful site....... :)

Haneep said...

thanks..

Anonymous said...

if i want to have multiple keys in a single web.config in root but the keys may be duplicate & path is different for the value of keys is it possible??

Thanks in advance reply soon......

ANIL KUMAR REDDY said...

Excellent Blog Suresh,Really i like very much, keep rocking

Anonymous said...

What is the use of making multiple web.config files in Web Application and can we make different connection string in the different web.config files having the same key

Narendran said...

But,

i want to link two number of web application to another web application (that is to the third one.), to access that two apps data to the third one. Please do the needful.

Narendran
9940678414

Unknown said...

which is first working web.config root or subroot?
i want only one web.config call at the time what i am doing?
pls tell me

ravi said...

Really nice article..but i want to know that how to get the value defined in subroot web.config file from rott aspx page and similarly to get the value of root web.config file to subroot folder

rahul said...

sureshbhai your arrticles are very good.plz publih one article regrading how to do URL Rewritting in asp.net c# using httpmodules and using rewriter tag..Thanks in advance.....

Anonymous said...

I am searching for the Form authentication via the different web cofig.........Sorry, It is not use ful to me......

rajasekhar said...

Error:Server Error in '/webconfig' Application.

The application setting 'rootfolder' was not found in the applications configuration.

this kind error i got .i dont know what i do please help me

rajasekhar said...

Server Error in '/webconfig' Application.

The application setting 'rootfolder' was not found in the applications configuration.

i got this error .please help me.

Anonymous said...

Thanks for ur articles
it help us a lot

Unknown said...
This comment has been removed by the author.
Unknown said...

Not Usefull Two webconfig..
Two connection strings are usefull in one webcinfig..

Unknown said...

What is the use full in usage of multiple web config files in single application.

Thanks in advance.. i m waiting for you reply,

Anonymous said...

yes

Unknown said...

nice articles thaks sir

Anonymous said...

If we access root folder web config key value from sub folder page how we can access

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.