Work With External AppSetting Of Web.Config

In this article, you will learn the following things.

  • .NET Configuration Files.
  • What is AppSetting in 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 types of application and types of the configuration file used.

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 Configuration tag. All custom settings o a web application can be stored here.


  1.  <appSettings>  
  2.     <add key="[KeyName]" value="[Value of Key]"/>  
  3. </appSettings>  


  1. <configuration>  
  2.   <appSettings>  
  3.     <add key="MainPath" value="D:\Projects\GstInvoice\"/>  
  4. </appSettings>  
  5. </configuration>  

Please visit the following link for more details -

How to retrieve AppSetting Key’s Value?

To retrieve the key’s value from AppSetting section into page, you have to use ConfigurationManager Class that is derived from System.Configuration Namespace.


ConfigurationManager.AppSetting[ “Key Name”] 


String MainSitePath = ConfigurationManager.AppSettings["MainSite"] 

External AppSetting

We can store AppSetting in 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 intorduced 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 application.
  • Using configsource attribute we have to move all and entire appsetting to separate file and no merging.

For more detail visit following links,

Difference between ConfigSource vs File attribute 

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”


Switch to solution explorer or Press ALT + CTRL + L

You can see there is Web.config file.


Double click on Web.Config file following is the default view of Web.Config file:

As you can see in default view there is no appsetting element tag.

  1. <?xml version="1.0"?>  
  3. <!--  
  4.   For more information on how to configure your ASP.NET application, please visit  
  6.   -->  
  8. <configuration>  
  10.     <system.web>  
  11.       <compilation debug="true" targetFramework="4.5.2" />  
  12.       <httpRuntime targetFramework="4.5.2" />  
  13.     </system.web>  
  15. </configuration>  

Now you can see I had created simple internal AppSetting within Web.Config file. 

  1. <configuration>  
  3.   <appSettings>  
  4. <add key="intUserName" value="Ashish Kalla"/>  
  5.   </appSettings>  
  7.     <system.web>  
  8.       <compilation debug="true" targetFramework="4.5.2" />  
  9.       <httpRuntime targetFramework="4.5.2" />  
  10.     </system.web>  
  12. </configuration>  

Creating external AppSetting



Now I am going to write external app setting file called “ExternalAppSetting.config”

Code in ExternalAppSetting.config

  1. <?xml version="1.0"?>  
  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"?>  
  3. <!--  
  4.   For more information on how to configure your ASP.NET application, please visit  
  6.   -->  
  8. <configuration>  
  10.   <appSettings file="ExternalAppSetting.config">  
  11.     <add key="intUserName" value="Ashish Kalla"/>  
  12.   </appSettings>  
  14.     <system.web>  
  15.       <compilation debug="true" targetFramework="4.5.2" />  
  16.       <httpRuntime targetFramework="4.5.2" />  
  17.     </system.web>  
  19. </configuration>  

To check internal and external app settings insert a new webform file named “default.aspx”

Right click on project or project name


Select ADD --> ADD NEW ITEM --> WebForm

Named “Default.aspx” then click ADD button.


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 the file.

Code in Default.aspx

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>  
  3. <!DOCTYPE html>  
  5. <html xmlns="">  
  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;  
  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();  
  16.         Response.Write(Session["ram"]);  
  17.     }  
  18. }   



Up Next
    Ebook Download
    View all
    View all