(Reference: msdn, ASP.NET 4 unleashed)
Maintaining Application State
On web, you request for a web page using HTTP protocol and the web server that hosts the web page responds and sends back the page that you requested for. To the web server that hosts the web page, every request is a new request as HTTP protocol is stateless. It means that when you make repeated requests for the same web page from a web server, the web server always treats you as a stranger on every request. This looks like as if the web server is suffering from a short-term memory loss problem.Maintaining Application State Using Browser Cookies
Creating a cookie:
if (Response.Cookies["mycookie"] == null){
Response.Cookies["mycookie"].Value = "You just created a cookie";
}
Reading from a cookie://if (HttpContext.Current.Request.Cookies["modele"] != null){
if (Response.Cookies["mycookie"] != null){
string strMyCookieValue = Response.Cookies["mycookie"].Value;
}
Loop through the cookie collection:ArrayList cookieCollection = new ArrayList();
for (int i = 0; i < Response.Cookies.Count; i++){
cookieCollection.Add(Response.Cookies[i]);
}
Deleting cookiesTo delete an existing cookie, set its expiration date to a date in the past.
Response.Cookies["myCookie"].Expires = DateTime.Now.AddDays(-1);Multivalued Cookies
A multivalued cookie is a single cookie that contains subkeys.
if (Response.Cookies["Student"] == null){
Response.Cookie["Student"]["firstname"] = "Joe";
Response.Cookie["Student"]["lastname"] = "B";
Response.Cookie["Student"]["GPA"] = "3.9";
}
Maintaining Application State Using Session State
Limitations of a cookie:- Size limitation - A cookie can hold upto 4096 bytes.
- Max limit of cookies per application - A single domain should not contain more than 20 cookies.
- Restriction of type of data a cookie can hold: Only strings of text
- No size limitation
- Can accommodate from simple strings of text to complex objects
Session["mySessionItem"] = "Added Item";
- or -
Session.Add("mySessionItem","Added Item");
string mySessionItemValue = Session["mySessionItem"].ToString();
Session.Abandon();Clear all items from Session state:
Session.Clear();Remove a particular item from the Session state:
Session.Remove("mySessionItem");
Improving the performance of your ASP.NET applications by Caching Application Pages and Data
Different caching techniques in ASP.NET 4 Framework:- Page Output Caching
- Partial Page Caching
- DataSource Caching
- Data Caching