appSettings ExampleAccess AppSettings to get shared strings and other values. Use properties with AppSettings.
This page was last reviewed on Sep 26, 2022.
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.
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 site-wide variables.
Add values. 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.... -->
Example. 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.
Note RootTitle and RootName are defined and used. Look at how they are called with "this" in the Page_Load method.
Detail The code uses 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; } }
Cache. It is possible to cache the results from appSettings. This makes the code much faster and allows better code separation and clarity.
Global Variable
<?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>
Encapsulate settings. Using properties is ideal for these appSettings. But the properties are slow—they cause repeated NameValueCollection lookups.
So We can make a new class in App_Code. Static classes hold static methods and enforce code correctness.
Tip When the ASP.NET AppDomain for this site is initialized the properties will be accessed and cached.
And It has properties that are 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"]; } }
Access cached values. 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.
Thus We cached 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; }
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.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Sep 26, 2022 (edit).
© 2007-2024 Sam Allen.