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-.aspxSpecifying 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">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.<add key="MyKey" value="12"/></appSettings>
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.
Get solutions of .NET problems with video explanations, .pdf and source code in .NET How to's.
<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) {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.// 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;}
沒有留言:
張貼留言