Understanding
Cookie, Cache, Session, Application and Viewstate Objects
Developers always get
confused how and where to store their data when working with web
applications. This posting would help people to understand the
difference between each data holders in ASP.NET
Cookies
A cookie is a file created by an
Internet site to store information on your computer, such as your preferences
when visiting that site. For example, if you inquire about a flight schedule at
an airline's Web site, the site might create a cookie that contains your
itinerary. Or it might only contain a record of the pages you looked at within
the site you visited, to help the site customize the view for you the next time
you visit.
A persistent cookie is
one stored as a file on your computer, and it remains there even when you
close Internet Explorer. The cookie
can be read by the Web site that created it when you visit that site again.
In
.NET: HttpCookie (HttpCookieCollection),
found as a property of the Request (Collection of cookies sent by the
client) and Response class (Response cookie collection).
A temporary or session cookie is stored only for your current browsing session, and is deleted from your computer when you
close Internet Explorer.
Cache
An application's Cache object allows
you to store and retrieve arbitrary data on subsequent requests . The cache is
not specifically associated with a page or user session. It is used primarily
to enhance application performance and it remains valid as long as the application domain remains
active.
In
.NET: Cache, found as a property of the
Page class (is the actual cache object associated with the application in which
the page resides)
Session
This provides information about the
current request's session. A Session object is maintained for each user that
requests a page or document from an ASP.NET application. Variables stored in
the Session object are not discarded when the user moves from page to page in
the application; instead, these
variables persist as long as the user is accessing pages in your application.
In
.NET: HttpSessionState, found as a
property of the Page class
Application
Defines the methods, properties, and
events common to all application objects within an ASP.NET application. This
class is the base class for applications defined by the user in the global.asax
file. Enables sharing of global information across multiple sessions and
requests within an ASP.NET application.
Note: Application state is not
shared across either a Web farm (in which an application is hosted across
multiple servers) or a Web garden (in which an application is hosted across
multiple processes on the same computer).
In
.NET: HttpApplication, found as a
property of the Page class
Viewstate
View state for a page or control is
the cumulative propery values, or view, of that page or control. This class is
the primary storage mechanism for all HTML and Web server controls. It stores attribute/value pairs as
strings associated with the control.
It tracks changes to these attributes only after the OnInit method is executed
for a page request, and save the changes to the page's or control's view state.
You can read from this class during
any stage of the control processing lifecycle, but you should not write to it
while the control is rendering.
In
.NET: StateBag, found as a property of
the Page class
The following code would help:
private void Page_Load(object sender, System.EventArgs e)
{
string strDateVal = System.DateTime.Now.ToString();
//This Application value would remain the same for all sessions
if (Application.Get("ApplicationValue") == null)
Application.Add("ApplicationValue", "Application Value updated at " + strDateVal);
//This cookie value once set would remain the same for 7 days
if (Request.Cookies.Get("CookieValue") == null)
{
HttpCookie MyCookie = new HttpCookie("CookieValue", "Cookie Value updated at " + strDateVal);
//Expires after 7 days from the date of creation
MyCookie.Expires = DateTime.Now.Add(new TimeSpan(7,0,0));
Response.Cookies.Add(MyCookie);
}
//Check the same cache value by opening another web page instance in IE
if (Cache["CacheValue"] == null)
Cache["CacheValue"] = "Cache Value updated at " + strDateVal;
//Check the same session value in another page in the application and another web page instance in IE
if (Session["SessionValue"] ==null)
Session["SessionValue"] = "Session Value updated at" + strDateVal;
//Put a breakpoint and check here during PostBack
if (this.ViewSate["ViewStateValue"] ==null)
this.ViewState.Add("SessionValue", "Session Value updated at" + strDateVal);
}
protected override void Render(HtmlTextWriter writer)
{
writer.Write(Cache["CacheValue"] as string + "<br>");
writer.Write(Session["SessionValue"] as string + "<br>");
writer.Write(Request.Cookies.Get("CookieValue").Value + "<br>");
writer.Write(Application.Get("ApplicationValue").ToString() + "<br>");
writer.Write(this.ViewSate["ViewStateValue"] as string + "<br>");
base.Render (writer);
}
{
string strDateVal = System.DateTime.Now.ToString();
//This Application value would remain the same for all sessions
if (Application.Get("ApplicationValue") == null)
Application.Add("ApplicationValue", "Application Value updated at " + strDateVal);
//This cookie value once set would remain the same for 7 days
if (Request.Cookies.Get("CookieValue") == null)
{
HttpCookie MyCookie = new HttpCookie("CookieValue", "Cookie Value updated at " + strDateVal);
//Expires after 7 days from the date of creation
MyCookie.Expires = DateTime.Now.Add(new TimeSpan(7,0,0));
Response.Cookies.Add(MyCookie);
}
//Check the same cache value by opening another web page instance in IE
if (Cache["CacheValue"] == null)
Cache["CacheValue"] = "Cache Value updated at " + strDateVal;
//Check the same session value in another page in the application and another web page instance in IE
if (Session["SessionValue"] ==null)
Session["SessionValue"] = "Session Value updated at" + strDateVal;
//Put a breakpoint and check here during PostBack
if (this.ViewSate["ViewStateValue"] ==null)
this.ViewState.Add("SessionValue", "Session Value updated at" + strDateVal);
}
protected override void Render(HtmlTextWriter writer)
{
writer.Write(Cache["CacheValue"] as string + "<br>");
writer.Write(Session["SessionValue"] as string + "<br>");
writer.Write(Request.Cookies.Get("CookieValue").Value + "<br>");
writer.Write(Application.Get("ApplicationValue").ToString() + "<br>");
writer.Write(this.ViewSate["ViewStateValue"] as string + "<br>");
base.Render (writer);
}
No comments:
Post a Comment