Go to DotNetFunda.com
 Online : 1575 |  Welcome, Guest!   Login
 
Home > Articles > ASP.NET > External AppSetting file for your AppSettings in .config file

Submit Article | Articles Home | Search Articles |

External AppSetting file for your AppSettings in .config file

3 vote(s)
Rating: 4.67 out of 5
red flag  Posted on: 7/29/2010 5:05:59 AM by SheoNarayan | Views: 942 | Category: ASP.NET | Level: Beginner


This article describes how to specify an external AppSetting file from within web.config file and specify the settings value inside external file. It also describes related benefits and limitation of the external AppSetting file.



Introduction

Despite the fact that specifying external AppSetting file in the web.config file was available since the first version of .NET, it was not well known and used. In this article, we will see how to use External .config file to specify AppSetting keys and values.


 

Video of this article

 You can watch the video of this article at http://www.dotnetfunda.com/tutorials/videos/x70-external-appsetting-file-for-your-appsettings-in-config-file-.aspx

 

Specifying the External AppSetting file

In order to specify the external AppSetting file, you will have to use the file attribute of the AppSettings node.

<appSettings file="MyConfig.config">

<add key="MyKey" value="12"/>

</appSettings>

In the above code snippet, MyConfig.config file is my external config file that I can use to specify the key value pairs for my AppSetting.

Where can I use this?

You can use Exernal AppSetting file to separately maintain your AppSettings in order to avoid complexity of maintaining everything into a single web.config or app.config file. You can also use this external setting file to distinguish your settings between different development, staging and production environments.

Point of interest about External AppSetting file

  • You can keep your appSettings keys and values both in your web.config or app.config or your external AppSetting file.
  • If you specify the same key into your web.config file and external AppSetting file (MyConfig.config in my case, the web.config key value will be overridden by the external AppSetting file key value.
  • The extension name of the external AppSetting file need not be a .config, you can specify .xml or .config1 etc as well. However it is suggested to keep the extension name as .config as .config is protected from external world and it can't be downloaded.
  • You will not be able to specify more than one external AppSettings file from your web.config file, as duplicate settings nodes inside web.config is not allowed.
  • You can not specify another external AppSetting file from within the first external AppSetting file (for example: I can't specify another external AppSetting file MyConfig1.config file from MyConfig.config file). If you do that, you will get following error.
Unrecognized attribute 'file'. Note that attribute names are case-sensitive. (C:\Mukul\WebSite1\MyConfig.config line 2)
  • If you modify the key value of your external AppSetting file while application is running, the change will not get affected unless you restart your application.
  • The external AppSetting config file must contain only AppSetting node or you will get an error.
  • You can keep this External AppSetting file in any of the subfolder and you can specify the path something like below. Here, I have kept my conifg file into Config folder under root.

<appSettings file="Config/MyConfig.config">

How to specify appSettings in the External AppSetting file?

You can specify the appSettings keys and values into your External AppSetting file in the same way, you specify in the web.config or app.config file. Below is the code snippet of my external appSetting config file.

<?xml version="1.0"?>

<appSettings>

<add key="MyKey1" value="MyValue1 - coming from Exernal MyConfig.config"/>

</appSettings>

How to retrieve values of the external AppSetting file?

Easy and no need any extra effort, just retrieve it the way you retrieve from your appSettings inside web.config or app.config file. Of course, as usual you will have to use using System.Configuration; namespace.

protected void Page_Load(object sender, EventArgs e)

{

// get from web.config

string myKey = ConfigurationManager.AppSettings.Get("MyKey");

lblMessage.Text += "<b>AppSetting value from web.config: </b>" + myKey;

 

// get from external AppSetting file

myKey = ConfigurationManager.AppSettings.Get("MyKey1");

lblMessage.Text += "<br /><b>AppSetting value from external AppSetting file: </b>" + myKey;

}

In the above code snippet, first time myKey variable is set with web.config appSetting key value and second time it is set with external MyConfig.config file appSetting key value.

Conclusion

Hope you like this article small article on external AppSetting file. Keep learning and sharing your knoweldge on this website.

 You can watch the video recording of this topic at http://www.dotnetfunda.com/tutorials/videos/x70-external-appsetting-file-for-your-appsettings-in-config-file-.aspx


If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Found interesting? Add this to:

| More



Please Sign In to vote for this post.

 
Latest post(s) from SheoNarayan

Latest Articles

About Sheo Narayan

Experience:8 year(s)
Home page:http://www.snarayan.com
Member since:Tuesday, July 08, 2008
Level:HonoraryPlatinum
Status: [Administrator]
Biography:Throughout 1st in all educational exams.
Major qualifications: HDCS, ADCA, MCA, MCTS
Developing and architecting applications in Microsoft technologies since year 2001.
Location: Hyderabad, India
 Response(s)
Posted by: Poster | Posted on: 31 Jul 2010 10:05:14 PM

I didn't know about it that we can even specify external AppSetting file. Its cool and hardly known by people.

Thanks for sharing this.

Submit Article

About Us | The Team | Advertise | Contact Us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you found plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 9/7/2010 12:13:32 AM