C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Example keys:
SiteName
SiteTitle
Example values:
New York Times
Breaking News, World News, and Multimedia
Web.config that uses appSettings: XML
<?xml version="1.0"?>
<configuration>
<configSections>
<!-- some stuff omitted here -->
<appSettings>
<add key="SiteTitle" value="Breaking News, World News, and Multimedia"/>
<add key="SiteName" value="New York Times"/>
</appSettings>
<!-- more stuff.... -->
Info: This example includes System.Web.Configuration. Note the using line at the top where this namespace is included.
Note: RootTitle and RootName are defined and used. Look at how they are called with "this" in the Page_Load method.
AppSettings: The code uses string lookup key on AppSettings to get the correct value. It is a NameValueCollection, a "specialized" hashtable.
NameValueCollectionPage that uses AppSettings: C#
using System;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.Configuration;
public partial class _Default : Page
{
public string RootTitle
{
get
{
// Returns the title string.
return WebConfigurationManager.AppSettings["SiteTitle"];
}
}
public string RootName
{
get
{
return WebConfigurationManager.AppSettings["SiteName"];
}
}
protected void Page_Load(object sender, EventArgs e)
{
// Access the properties (and thus Web.config) like this.
this.Title1.Text = this.RootName + " - " + this.RootTitle;
}
}
Web.config appSettings keys: XML
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="SiteTitle" value="The New York Times - Breaking News..."/>
<add key="SitePrefix" value="NYT"/>
<add key="SiteHeader" value="Breaking News, World News & Mutimedia"/>
<add key="BaseUrl" value="http://nytimes.com/"/>
</appSettings>
Static: Static classes hold static methods and enforce code correctness. There is a static constructor that sets the properties.
Tip: 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.
SiteGlobal class that caches appSettings: C#
using System;
using System.Web;
using System.Web.Configuration;
public static class SiteGlobal
{
/// <summary>
/// Full site title tag at root.
/// </summary>
static public string RootTitle { get; set; }
/// <summary>
/// Header prefix on root page.
/// </summary>
static public string RootPrefix { get; set; }
/// <summary>
/// Header main part on root page.
/// </summary>
static public string RootHeader { get; set; }
/// <summary>
/// Main site Url with http://.
/// </summary>
static public string BaseUrl { get; set; }
static SiteGlobal()
{
// Cache all these values in static properties.
RootTitle = WebConfigurationManager.AppSettings["SiteTitle"];
RootPrefix = WebConfigurationManager.AppSettings["SitePrefix"];
RootHeader = WebConfigurationManager.AppSettings["SiteHeader"];
BaseUrl = WebConfigurationManager.AppSettings["BaseUrl"];
}
}
Review: We cached appSettings in ASP.NET with a static class and static properties. This provides clearer code and improves performance.
Example Page_Load event handler: C#
protected void Page_Load(object sender, EventArgs e)
{
Title1.Text = SiteGlobal.RootTitle;
Prefix1.InnerHtml = SiteGlobal.RootPrefix;
Header1.InnerHtml = SiteGlobal.RootHeader;
}