Work With External AppSetting Of Web.Config

In this article, you will learn the following things.
  • .NET Configuration Files.
  • What is AppSetting in the web.config file?
  • How to retrieve AppSetting Key’s Value?
  • External appsetting
  • Difference between FILE and CONFIGSOURCE attribute of AppSettings
  • Step by Step Implementation using FILE attribute of APPSETTINGS
.NET Configuration Files
 
Types of configuration files in DOT NET (.NET) framework: In .NET, we can develop the following types of applications and for all types of applications, we require a configuration file. In the below chart, I have explained the types of applications and types of configuration files used.
 
TYPES OF APPLICATION CONFIGURATION FILENAME
Console Application App.Config
WinForm Application App.Config
Web Application (Web Form / MVC) Web.Config
WPF Application App.Config
 
What is AppSetting in web.config file?
 
As the name defines itself, AppSetting means Application Setting. In the section, we store settings in a pair of Key/Value. AppSetting element section exists under the Configuration tag. All custom settings o a web application can be stored here.
 
Syntax 
  1.  <appSettings>  
  2.     <add key="[KeyName]" value="[Value of Key]"/>  
  3. </appSettings>  
Example
  1. <configuration>  
  2.   <appSettings>  
  3.     <add key="MainPath" value="D:\Projects\GstInvoice\"/>  
  4. </appSettings>  
  5. </configuration>  
Please visit the following link for more details - 
 
https://msdn.microsoft.com/en-us/library/ms228154(v=vs.100).aspx
 
How to retrieve AppSetting Key’s Value?
 
To retrieve the key’s value from the AppSetting section on the page, you have to use ConfigurationManager Class that is derived from System.Configuration Namespace.
 
Syntax
 
ConfigurationManager.AppSetting[ “Key Name”] 
 
Example
 
String MainSitePath = ConfigurationManager.AppSettings["MainSite"] 
 
External AppSetting
 
We can store AppSetting in an external file to manage it efficiently, besides that, at the Runtime, we can change the value of AppSetting and update the file without restarting the application. 
 
To store AppSetting externally, there are two ways-
  1. <appSettings file=””>
  2. <appSettings configsource=””>
Difference between File and ConfigSource attribute of AppSettings
 
File attribute
  • This attribute was introduced in .Net Framework version 1.1.
  • Using file attribute and separate file allow us to update in appSetting section without restart application.
  • The appsetting of separate files are merged with the appSetting section of the Web.Config
ConfigSource attribute
  • This attribute introduced in .Net Framework version 2.0
  • Using configsource attribute any changes cause to restart the application.
  • Using configsource attribute we have to move all and entire appsetting to separate files and no merging.
For more detail visit the following links,
 
 
Step by Step Implementation using FILE attribute of APPSETTINGS
 
Start Visual Studio(VS) I am using VS Community 2015
 
Click on File-->New-->WebSite
 
Named project as “ExternalAppSetting”
 
.NET
 
Switch to solution explorer or Press ALT + CTRL + L
 
You can see there is Web.config file.
 
.NET
 
Double click on Web.Config file following is the default view of the Web.Config file:
 
As you can see in the default view there is no appsetting element tag.
  1. <?xml version="1.0"?>  
  2.   
  3. <!--  
  4.   For more information on how to configure your ASP.NET application, please visit  
  5.   http://go.microsoft.com/fwlink/?LinkId=169433  
  6.   -->  
  7.   
  8. <configuration>  
  9.   
  10.     <system.web>  
  11.       <compilation debug="true" targetFramework="4.5.2" />  
  12.       <httpRuntime targetFramework="4.5.2" />  
  13.     </system.web>  
  14.   
  15. </configuration>  
Now you can see I had created a simple internal AppSetting within Web.Config file. 
  1. <configuration>  
  2.   
  3.   <appSettings>  
  4. <add key="intUserName" value="Ashish Kalla"/>  
  5.   </appSettings>  
  6.     
  7.     <system.web>  
  8.       <compilation debug="true" targetFramework="4.5.2" />  
  9.       <httpRuntime targetFramework="4.5.2" />  
  10.     </system.web>  
  11.   
  12. </configuration>  
Creating external AppSetting
 
.NET
 
.NET
 
Now I am going to write an external app setting file called “ExternalAppSetting.config”
 
Code in ExternalAppSetting.config
  1. <?xml version="1.0"?>  
  2.   
  3.   <appSettings>  
  4.     <add key="extUserName" value="Suhana Kalla"/>      
  5.   </appSettings>  
Now linking external app setting config file to web.config. As you know there two method to attached external appsetting config file. Two attributes are configsource or file.
 
Web.Config file code
 
For attaching external appsetting with file attribute,
  1. <?xml version="1.0"?>  
  2.   
  3. <!--  
  4.   For more information on how to configure your ASP.NET application, please visit  
  5.   http://go.microsoft.com/fwlink/?LinkId=169433  
  6.   -->  
  7.   
  8. <configuration>  
  9.   
  10.   <appSettings file="ExternalAppSetting.config">  
  11.     <add key="intUserName" value="Ashish Kalla"/>  
  12.   </appSettings>  
  13.     
  14.     <system.web>  
  15.       <compilation debug="true" targetFramework="4.5.2" />  
  16.       <httpRuntime targetFramework="4.5.2" />  
  17.     </system.web>  
  18.   
  19. </configuration>  
To check internal and external app settings insert a new webform file named “default.aspx”
 
Right-click on project or project name
 
.NET
 
Select ADD --> ADD NEW ITEM --> WebForm
 
Named “Default.aspx” then click the ADD button.
 
.NET
 
Double click on Default.aspx and insert two label controls and switch to code-behind file by pressing F7 or double click on default.aspx.cs file in solution explorer.
 
Before start coding first attached using System.Configuration; namespace at the top of the file.
 
Code in Default.aspx
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>  
  2.   
  3. <!DOCTYPE html>  
  4.   
  5. <html xmlns="https://www.w3.org/1999/xhtml">  
  6. <head runat="server">  
  7.     <title></title>  
  8. </head>  
  9. <body>  
  10.     <form id="form1" runat="server">  
  11.     <div>  
  12.     <b style="font-size:larger">Internal AppSetting</b>  
  13.         <br />  
  14.         <b>KEY</b>= intUserName and <b>Value</b> = <asp:Label ID="lblInternalAppsettingValue" runat="server" Text="Label"></asp:Label>  
  15.         <br />  
  16.         <br />  
  17.         <br />  
  18.     <b style="font-size:larger">External AppSetting</b>  
  19.         <br />  
  20.         <b>KEY</b>= extUserName and <b>Value</b> = <asp:Label ID="lblExternalAppsettingValue" runat="server" Text="Label"></asp:Label>  
  21.     </div>  
  22.     </form>  
  23. </body>  
  24. </html>  
Code in Default.aspx.cs
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.Configuration;  
  8.   
  9. public partial class _Default : System.Web.UI.Page  
  10. {  
  11.     protected void Page_Load(object sender, EventArgs e)  
  12.     {  
  13.         lblInternalAppsettingValue.Text = ConfigurationManager.AppSettings["intUserName"].ToString();  
  14.         lblExternalAppsettingValue.Text = ConfigurationManager.AppSettings["extUserName"].ToString();  
  15.   
  16.         Response.Write(Session["ram"]);  
  17.     }  
  18. }   
OUTPUT 
 
.NET