This step-by-step article demonstrates how to store
application-wide data through the
Application,
Cache, and
AppSettings objects. Application-wide data is data that is available to the
entire Web application.
Using the Application Object
- Create a new Visual C# ASP.NET Web application.
- Add a TextBox control to WebForm1.aspx. TextBox1 is created by default.
- Add the following code to the Page_Load event:
TextBox1.Text = Application["abc"].ToString();
- Add the following code to the Application_Start event in the code-behind module for the Global.asax file:
Application["abc"] = "hi";
- Compile the project.
- View WebForm1.aspx in your browser.
Using the Cache Object
- Create a new Visual C# ASP.NET Web application.
- Add a TextBox control to WebForm1.aspx. TextBox1 is created by default.
- Add the following code to the Page_Load event:
TextBox1.Text = Cache["abc"].ToString();
- Add the following code to the Application_Start event in the code-behind module for the Global.asax file:
Context.Cache.Insert ("abc", "Hello", null, DateTime.MaxValue, TimeSpan.Zero);
- NOTE: When you use the Cache object inside the Global.asax file, you must access it through
the Context object, such as Context.Cache.
- Compile the project.
- View WebForm1.aspx in your browser.
Using the AppSettings Object
- Create a new Visual C# ASP.NET Web application.
- Add a TextBox control to WebForm1.aspx. TextBox1 is created by default.
- Add the following code to the Page_Load event:
TextBox1.Text = ConfigurationSettings.AppSettings["abc"].ToString();
- Add the following code at the top of the same code-behind
file:
using System.Configuration;
- In the Web.config file, locate the following section:
- Add the following text just below <configuration>.
Note that appSettings is case sensitive.
<appSettings>
<add key="abc" value="Hello World" />
</appSettings>
- Compile the project.
- View WebForm1.aspx in your browser.
Additional Notes
- If you modify either the Global.asax or the Web.config file
(even with Notepad), the Web application is restarted. This purges all data
that is stored in memory, such as application and cache data, as well as
session data if it is stored in memory (that is, if you are using InProc
session state mode).
- The Cache object has sophisticated memory management features. For
demonstration purposes, this article uses DateTime.MaxValue for absolute expiration. Thus, the code does not optimally
leverage these memory management features.For additional information the Cache
object, click the article number below to view the article in the Microsoft
Knowledge Base:
307225 INFO: ASP.NET Caching Overview
- How do you know which method is the best for you? Why would
you select one method over another?
The ability to modify the value
of a variable in code (read/write) while the application is running can be both
a benefit and a detriment. This is detrimental if multiple processors (CPUs) or
multiple servers are running your Web application. Each CPU or server maintains
its own value for the variable. If you change the value of one, your change
does not affect any of the other values.
- Using the Cache object:
Read/Write - You can add or modify items in
code while the application is running. To mimic read-only code, set the
variable's value in the Application_Start event and then do not change it anywhere else.
The Cache object becomes more interesting when you use its memory
management features. If you have data that needs to be refreshed either
regularly or occasionally, this object has many time-based and dependency-based
features to invoke a callback function, which you can use to refresh data.
Alternately, if some function generates a large amount of data that may (or may
not) get reused, the cache can hold it at low priority and purge it if memory
is needed for something else. - Using the Application object:
Read/Write - You can add or modify items in
code while the application is running. To mimic read-only code, set the
variable's value in the Application_Start event and then do not change it anywhere else.
Application variables are popular for storing static items that need to be
globally accessible and yet modifiable at run time. - Using the AppSettings object:
Read only - Items are read in from the
Web.config file when the application first starts. You cannot add or modify
items without restarting the application.
Similar to Application object variables, AppSettings are most popular for items that remain static and are easier to
maintain in one place.