Wednesday, November 23, 2011

How to use more than one web.config in asp.net

By default when a webspplication in created we get root folder web.config
set the appsettings tag as follows
(Iam removing the start and end tag symbols due to publishing problems,instead i use '(' and ')' in place of '<' and '>'.Please replace while you test this code)
(appsettings)
(add key="rootfolder" value="Root Folder web.config"/)
(add key="test1" value="Testing the application for root folder web.config"/)
(/appSettings )

in Default.aspx
(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)

Run your application
Output:
AppKey value:Root Folder web.config
AppKey Test value:Testing the application for root folder web.config

Create SubRoot Folder and add web.config and one aspx page and name it as test.aspx

add settings in web.config in subroot folder
(appSettings)
(add key="subrootfolder" value="Sub Root Folder web.config"/)
(add key="test1" value="Testing the application for sub root folder web.config"/)
(/appSettings )

in Test.aspx
(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)

Run application and test.aspx as start page
Output
AppKey value:Sub Root Folder web.config
AppKey Test value:Testing the application for sub root folder web.config

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.

No comments:

Post a Comment