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.

234E3Q keys:4 SiteName SiteTitle 4E3Q 3ds:4 New York Times Breaking News, World News,3RMultimedia4Web.config that uses appSettings: XML4 <?xml version="1.0"?> <configuration> 2<configSections>4 2<!-- some stuff omitted here --> 24<appSettings> 22<add key="SiteTitle" 3d="Breaking News, World News,3RMultimedia"/> 22<add key="SiteName" 3d="New York Times"/> 2</appSettings>4 2<!-- more stuff.3m-->4Page that uses AppSettings: C#4 3P3O; 3P3O.Web.UI; 3P3O.Web.UI.HtmlControls; 3P3O.Web.Configuration3S3=partial 3D_Default : Page { 23=3L RootTitle 2{ 22get 22{43'3{Returns the title 3L.3'43J WebConfigurationManager.4AppSettings4[4"SiteTitle"4]; 22} 2} 23=3L RootName 2{ 22get 22{3'3J WebConfigurationManager.4AppSettings4[4"SiteName"4]; 22} 2} 2protected 3fPage_Load(object sender, EventArgs e) 2{4 223{Access the properties (and thus Web.config) like this. 224this.Title1.Text3zthis.RootName3y" - "3ythis.RootTitle; 2} }4Web.config appSettings keys: XML4 <?xml version="1.0"?> <configuration> 24<appSettings>4 22<add key="SiteTitle" 3d="The New York Times - Breaking News..."/> 22<add key="SitePrefix" 3d="NYT"/> 22<add key="SiteHeader" 3d="Breaking News, World News & Mutimedia"/> 22<add key="BaseUrl" 3d="http://nytimes.com/"/> 24</appSettings>4SiteGlobal 3Dthat caches appSettings: C#4 3P3O; 3P3O.Web; 3P3O.Web.Configuration3S3=3A3DSiteGlobal {4 2/3{<3B> 2/3{Full site title tag at root. 2/3{</3B> 243A3=3L RootTitle { get; set; }4 2/3{<3B> 2/3{Header prefix on root page. 2/3{</3B> 243A3=3L RootPrefix { get; set; }4 2/3{<3B> 2/3{Header main part on root page. 2/3{</3B> 243A3=3L RootHeader { get; set; }4 2/3{<3B> 2/3{3v site Url with http://. 2/3{</3B> 243A3=3L BaseUrl { get; set; } 23ASiteGlobal() 2{4 223{Cache all these 3ds3k3Aproperties. 224RootTitle3zWebConfigurationManager.AppSettings[4"SiteTitle"4]; 22RootPrefix3zWebConfigurationManager.AppSettings[4"SitePrefix"4]; 22RootHeader3zWebConfigurationManager.AppSettings[4"SiteHeader"4]; 22BaseUrl3zWebConfigurationManager.AppSettings[4"BaseUrl"4]; 2} }4E3Q Page_Load event handler: C#4 protected 3fPage_Load(object sender, EventArgs e) { 2Title1.Text3zSiteGlobal.RootTitle; 2Prefix1.InnerHtml3zSiteGlobal.RootPrefix; 2Header1.InnerHtml3zSiteGlobal.RootHeader; }4