AppSettings
This stores strings or other values. It is found in a section of Web.config in an ASP.NET website project. We use this feature in the Web.config file.
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 site-wide variables.
Here we add markup to the Web.config file. First, open your Web.config file. Find
the configuration container tag, and add an appSettings
section.
<?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.... -->
Here we use the AppSettings
collection. We will use C# code to access the properties. This example includes System.Web.Configuration
—note the using statement at the top.
RootTitle
and RootName
are defined and used. Look at how they are called with "this" in the Page_Load
method.string
lookup key on AppSettings
to get the correct value. It is a NameValueCollection
, a "specialized" hashtable.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; } }
It is possible to cache the results from appSettings
. This makes the code much faster and allows better code separation and clarity.
<?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 & Multimedia"/> <add key="BaseUrl" value="http://nytimes.com/"/> </appSettings>
Using properties is ideal for these appSettings
. But the properties are slow—they cause repeated NameValueCollection
lookups.
class
in App_Code
. Static classes hold static
methods and enforce code correctness.AppDomain
for this site is initialized the properties will be accessed and cached.static
and public—properties can be static
just like variables and methods.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"]; } }
We can call into the SiteGlobal
class
and use its properties for access to the appSettings
. Using the cached values is faster, and will reduce memory pressure.
appSettings
in ASP.NET with a static
class
and static
properties. This provides clearer code and improves performance.protected void Page_Load(object sender, EventArgs e) { Title1.Text = SiteGlobal.RootTitle; Prefix1.InnerHtml = SiteGlobal.RootPrefix; Header1.InnerHtml = SiteGlobal.RootHeader; }
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.