ASP.NET appSettings ExampleAccess AppSettings to get shared strings and other values. Use properties with AppSettings.
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 keys:
SiteName SiteTitle
New York Times Breaking News, World News, and Multimedia
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
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.
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.... -->
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.
Page 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; } }
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 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.
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>
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.
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"]; } }
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.
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; }
© 2007-2022 sam allen.
see site info on the changelog.