.Net References

(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 the Application State means associating data (by caching the data either on the client or on server side) with a particular user over multiple user requests. ASP.NET provides many features for storing(caching) data both in the client (browser) and the server (web server) sides.

 

Maintaining Application State Using Browser Cookies


 
To create, modify, collect from, and delete the cookies, Response.Cookies collection is used.
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 cookies
To 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

 
Advantages of Session state:
  • No size limitation
  • Can accommodate from simple strings of text to complex objects
HttpSessionState class provides access to session-state values as well as session-level settings and lifetime management methods. HttpSessionState object is exposed by Page.Session, Context.Session, UserControl.Session, WebService.Session and Application.Session properties. This means that you can access Session state from just about anywhere.

 
Add an item to a Session object
Session["mySessionItem"] = "Added Item";
- or -
Session.Add("mySessionItem","Added Item");

 
Reading an item from a Session object
string mySessionItemValue = Session["mySessionItem"].ToString();

 
Abandon the user session programmatically:

 
Once the Abandon method is called, the current session is no longer valid and a new session can be started. Abandon causes the End event to be raised. A new Start event will be raised on the next request.
Session.Abandon(); 
Clear all items from Session state:
Session.Clear(); 
Remove a particular item from the Session state:
Session.Remove("mySessionItem"); 

 
Maintaining Application State Using Cookieless Session State

 
Maintaining Application State Using Profiles

 

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