In this article, you will learn the following things.
.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
ConfigSource attribute
For more detail visit the 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”
.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. For more information on how to configure your ASP.NET application, please visit
  4. http://go.microsoft.com/fwlink/?LinkId=169433
  5. -->
  6. <configuration>
  7. <system.web>
  8. <compilation debug="true" targetFramework="4.5.2" />
  9. <httpRuntime targetFramework="4.5.2" />
  10. </system.web>
  11. </configuration>
Now you can see I had created a simple internal AppSetting within Web.Config file.
  1. <configuration>
  2. <appSettings>
  3. <add key="intUserName" value="Ashish Kalla"/>
  4. </appSettings>
  5. <system.web>
  6. <compilation debug="true" targetFramework="4.5.2" />
  7. <httpRuntime targetFramework="4.5.2" />
  8. </system.web>
  9. </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. <appSettings>
  3. <add key="extUserName" value="Suhana Kalla"/>
  4. </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. For more information on how to configure your ASP.NET application, please visit
  4. http://go.microsoft.com/fwlink/?LinkId=169433
  5. -->
  6. <configuration>
  7. <appSettings file="ExternalAppSetting.config">
  8. <add key="intUserName" value="Ashish Kalla"/>
  9. </appSettings>
  10. <system.web>
  11. <compilation debug="true" targetFramework="4.5.2" />
  12. <httpRuntime targetFramework="4.5.2" />
  13. </system.web>
  14. </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. <!DOCTYPE html>
  3. <html xmlns="https://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5. <title></title>
  6. </head>
  7. <body>
  8. <form id="form1" runat="server">
  9. <div>
  10. <b style="font-size:larger">Internal AppSetting</b>
  11. <br />
  12. <b>KEY</b>= intUserName and <b>Value</b> = <asp:Label ID="lblInternalAppsettingValue" runat="server" Text="Label"></asp:Label>
  13. <br />
  14. <br />
  15. <br />
  16. <b style="font-size:larger">External AppSetting</b>
  17. <br />
  18. <b>KEY</b>= extUserName and <b>Value</b> = <asp:Label ID="lblExternalAppsettingValue" runat="server" Text="Label"></asp:Label>
  19. </div>
  20. </form>
  21. </body>
  22. </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. public partial class _Default : System.Web.UI.Page
  9. {
  10. protected void Page_Load(object sender, EventArgs e)
  11. {
  12. lblInternalAppsettingValue.Text = ConfigurationManager.AppSettings["intUserName"].ToString();
  13. lblExternalAppsettingValue.Text = ConfigurationManager.AppSettings["extUserName"].ToString();
  14. Response.Write(Session["ram"]);
  15. }
  16. }
OUTPUT
.NET