Microsoft Dot Net Master

Microsoft Dot Net Master
Microsoft Dot Net Master

Friday, July 1, 2011

How To Use Multiple Web.Config Files in Asp.Net 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
OUTPUT 


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
OUTPUT 
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

No comments:

Post a Comment