Monday, June 20, 2011

How To use two Web.Config Files in Asp.Net Application?

This tip’s will help you to maintain two web.config files in an Asp.Net web application. The known factor is, by default, every web application will have single config file named as “Web.Config”. You can add keys and values under the <appSettings/> tag and retrieve it from the web application code-behind. For some additional purpose, the application can have more than one config file. But the application can recognize the file named as “Web.Config” or the only config file available in the application. So let us examine how we can make the application to retrieve the values from the second config file.

Step 1: In the Asp.Net web application, add the second web.config file named as “Web2.config”
Step 2: Remove all the default tags in Web2.Config file and add only <appSettings/> tag and its key-value as follows.
<appSettings>
<add key="TestKey" value="Test Value From Web2.Config"/>
</appSettings>
Step 3: In the default Web.Config file, at the <appSettings> tag, add an attribute as ‘file="Web2.config"’ as follows.
<appSettings file="Web2.config">
</appSettings>
Step 4: Now access the value from the second config file as usual.
Response.Write(ConfigurationManager.AppSettings.Get("TestKey"));

No comments :

Post a Comment