AppSettings` stores strings or other values. It is found in a section of Web.config in an ASP.NET website project. Keeping constants in non-code files allows changes to be made easier. We use this feature in the Web.config file.`Intro.` We will use an AppSettings block in Web.config, which is a text file that ASP.NET treats specially for your project. It is good for keeping strings such as site titles, certain other site-wide variables or page names. `Add values.` Here we add the appropriate markup to the Web.config file. First, open your Web.config file in the Solution Explorer. Find the <configuration> container tag, and add an <appSettings> section. `Example.` Here we use the AppSettings collection. One good way to use AppSettings in the C# code is to encapsulate the values in properties. We will use the C# getter syntax to access the above properties. `Encapsulate Field `encapsulate-field`Info: `This example includes System.Web.Configuration. Note the using line at the top where this namespace is included.`Property usage.` Above, RootTitle and RootName are defined and used. Look at how they are called with "this" in the Page_Load method. That is a good way to use properties like these without causing confusion. `Finally: `It uses string lookup key on AppSettings to get the correct value. It is a NameValueCollection, a "specialized" hashtable.`NameValueCollection `namevaluecollection`Cache.` It is possible to cache the results from appSettings. This makes the code 30 times faster and allows better code separation and clarity. We will use ASP.NET global variables. Here are the settings in Web.config. `Encapsulate settings.` Using properties is ideal for these appSettings. My first appSettings article demonstrates this. But the properties are slow—they cause repeated NameValueCollection lookups. We can make a new class in App_Code. `Static classes` hold static methods and enforce code correctness. There is a static constructor that sets the properties. When the ASP.NET AppDomain for this site is initialized the properties will be accessed and cached. `Properties: `It has properties that are static and public. Properties can be static just like variables and methods.`Access cached values.` Now we can simply call into the SiteGlobal class and use the properties there for access to the appSettings. Using the cached values is 30 times faster, and will reduce memory pressure. `Review: `We cached appSettings in ASP.NET with a static class and static properties. This provides clearer code and improves performance.`Summary.` We used Web.config and appSettings to store values that are specific to the current deployment. These should not be frequently changed, but be separately stored from the code. This reduces complexity. `Finally: `Consider caching AppSettings for speed. This can reduce the time needed to render a page.

OVKqQEVF keys:QKKSiteNameKSiteTitleKKQEVF Vhs:QKKNew York TimesKBreaking News, World News,VVMultimediaQWeb.config that uses appSettings: XMLQKK<?xml version=q1.0q?>K<configuration>KO<configSections>QKO<!-- some stuff omitted here -->KOQ<appSettings>KOO<add key=qSiteTitleq Vh=qBreaking News, World News,VVMultimediaq/>KOO<add key=qSiteNameq Vh=qNew York Timesq/>KO</appSettings>QKO<!-- more stuff.Vl-->QPage that uses AppSettings: C#QKKVDVQ;KVDVQ.Web.UI;KVDVQ.Web.UI.HtmlControls;KVDVQ.Web.ConfigurationVbVBpartial VJ_Default : PageK{KOVBVP RootTitleKO{KOOgetKOO{QKOOOV{Returns the title VP.KOOOQVK WebConfigurationManager.QAppSettingsQ[QqSiteTitleqQ];KOO}KO}KKOVBVP RootNameKO{KOOgetKOO{KOOOVK WebConfigurationManager.QAppSettingsQ[QqSiteNameqQ];KOO}KO}KKOprotected VcPage_Load(object sender, EventArgs e)KO{QKOOV{Access the properties (and thus Web.config) like this.KOOQthis.Title1.TextVythis.RootNameV}q - qV}this.RootTitle;KO}K}QWeb.config appSettings keys: XMLQKK<?xml version=q1.0q?>K<configuration>KOQ<appSettings>QKOO<add key=qSiteTitleq Vh=qThe New York Times - Breaking News...q/>KOO<add key=qSitePrefixq Vh=qNYTq/>KOO<add key=qSiteHeaderq Vh=qBreaking News, World News & Mutimediaq/>KOO<add key=qBaseUrlq Vh=q>KOQ</appSettings>QSiteGlobal VJthat caches appSettings: C#QKKVDVQ;KVDVQ.Web;KVDVQ.Web.ConfigurationVbVBV?VJSiteGlobalK{QKO/V{<V7>KO/V{Full site title tag at root.KO/V{</V7>KOQV?VBVP RootTitle { get; set; }QKKO/V{<V7>KO/V{Header prefix on root page.KO/V{</V7>KOQV?VBVP RootPrefix { get; set; }QKKO/V{<V7>KO/V{Header main part on root page.KO/V{</V7>KOQV?VBVP RootHeader { get; set; }QKKO/V{<V7>KO/V{Vr site Url with http://.KO/V{</V7>KOQV?VBVP BaseUrl { get; set; }KKOV?SiteGlobal()KO{QKOOV{Cache all these VhsVpV?properties.KOOQRootTitleVyWebConfigurationManager.AppSettings[QqSiteTitleqQ];KOORootPrefixVyWebConfigurationManager.AppSettings[QqSitePrefixqQ];KOORootHeaderVyWebConfigurationManager.AppSettings[QqSiteHeaderqQ];KOOBaseUrlVyWebConfigurationManager.AppSettings[QqBaseUrlqQ];KO}K}QEVF Page_Load event handler: C#QKKprotected VcPage_Load(object sender, EventArgs e)K{KOTitle1.TextVySiteGlobal.RootTitle;KOPrefix1.InnerHtmlVySiteGlobal.RootPrefix;KOHeader1.InnerHtmlVySiteGlobal.RootHeader;K}Q