Click here to Skip to main content
15,072,422 members
Articles / Web Development / ASP.NET
Technical Blog
Posted 25 May 2011


46 bookmarked

Few important tips that you should know while using ASP.NET Session

Rate me:
Please Sign up or sign in to vote.
4.79/5 (30 votes)
25 May 2011CPOL4 min read
Few important tips while using ASP.NET Session

While working with ASP.NET web applications, you must be familiar with one of most important state management techniques “Session”. If you want to do a quick refresh or want to know something more, please go ahead and read one of my articles “Exploring Session in ASP.NET” published on CodeProject. In this post, I am going to share some important tips that might be useful.

ASP.NET Session State is on by default, hence you are paying for memory even if you don’t use it. There are several ways to optimize it.

Tip #1: Not using Session State at all? Then turn it off completely in web.config.


Tip #2: Session is only required for few pages, not all over the application.

Then first turn it off for all pages, for that you need to do the following entry in web.config.


Then enable it for a specific page where you required the session.


Tip #3: If you are using Session for Reading Purpose, use Session State as “ReadOnly”

If you are a beginner, you must be wondering what is EnableSessionState=”Readonly”. Well, if you look at your web application, not all the pages using Session or some of the pages are using session data for reading purposes. If there is no write operation happening on session, then it’s always better to use session State is “ReadOnly


The session requests pass through different httpModules within HTTPPipeline. To know more details on how session state ReadOnly works, please read the article Read Only Session State in ASP.NET. A quick summary from the referred article:

The session state module implements a readers – writers locking mechanism and queues the access to session state values. A page that has session-state write access will hold a writer lock on the session until the request finishes. A page gains write access to the session state by setting the EnableSessionState attribute on the @Page directive to True. A page that has session-state read access — for example, when the EnableSessionState attribute is set to ReadOnly — will hold a reader lock on the session until the request finishes.

You can also set ReadOnly SessionState in web.config as well:


Tip #4: Programmatically Change Session State Behavior when required (ASP.NET 4.0)

We can enable or disable session state either in web.config or using @Page directive’s EnableSessionState attributes. But there was no provision to change the session state at runtime till date in ASP.NET. But using ASP.NET 4.0, we can change the session state programmatically. The .NET 4.0 Framework adds a new method SetSessionStateBehavior to the HttpContext class for ASP.NET. This method required SessionStatebehavior value to set the current session mode. To call SetSessionStateBehavior, simply create a new HttpModule by implementing IHttModule and hook the BeginRequest event. Most important, you can only use the SetSessionStateBehavior until the AcquireRequestState event is fired, because AcquireRequestState occurs when ASP.NET acquires the current state that is associated with the current request.

While calling SetSessionStatebehavior, you can pass the following values as SessionStatebehavior:

  • Default: This is the default setting which means everything works as before.
  • Disabled: Turned of Session Sate for Current Request.
  • ReadOnly: Read only access to Session State.
  • Required: Enabled session state for both Read and Write Access.


Here is one of my complete articles, where I have discussed about the details of SetSessionStatebehavior which talks about the details implementation, use with real example.

Tip #5: Compress Session Data while using OutProc Session mode based on Requirements (ASP.NET 4.0)

ASP.NET 4.0 comes with a new option for compressing the Session data with Out Process Session mode. To enable this functionality, we need to add “compressionEnabled=”true” attribute with the SessionMode in web.config.


When Compression mode is enabled in web.config, ASP.NET compresses the serialized session data and passes it to session storage and during retrieval same deserialization and decompression happens in the server side. ASP.NET 4.0 used System.IO.Compression.GZStream class to compress the session mode.


This process has several advantages and disadvantages, please read my detailed article on Compression Enabled Session State and how its works, where I have explained how to use it for SQL Server and State Server Session Mode, how to calculate the size of Session Data and compare the amount of compressed data.

Tip #6: Use HttpContext.Current.Items for very short term storage instead of Session

You can use HttpContext.Current.Items for very short term storage. Short term storage means, this data is valid for a single HTTP Request. There is a lot of confusion regarding storing data in HttpContext.Current.Items and storing data in Session variable. Items collections of HttpContext is an IDictionary key-value collections and that are shared across a single HTTPRequest. Yes, HttpContext.Current.Items is valid for a single HTTPRequest.


Here is one of my complete blog posts, When we can use HttpContext.Current.Items to stores data in ASP.NET?, which describes the fundamentals of using HttpContext.Current.Items for storing short terms items instead of using session.

Hope the above tips will help you.



Filed under: .NET 4.0, ASP.NET 4.0, Tips and Tricks, Visual Studio, Visual Studio 2010


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Abhijit Jana
Technical Lead
India India
.NET Consultant | Former Microsoft MVP - ASP.NET | CodeProject MVP, Mentor, Insiders| Technology Evangelist | Author | Speaker | Geek | Blogger | Husband

Blog : http://abhijitjana.net
Web Site : http://dailydotnettips.com
Twitter : @AbhijitJana
My Kinect Book : Kinect for Windows SDK Programming Guide

Comments and Discussions

GeneralMy vote of 5 Pin
Shishpal Singh24-Jul-13 19:22
professionalShishpal Singh24-Jul-13 19:22 
GeneralMy vote of 5 Pin
Sanjay K. Gupta24-Apr-12 18:39
professionalSanjay K. Gupta24-Apr-12 18:39 
GeneralMy vote of 5 Pin
kamiktk12328-Mar-12 21:32
Memberkamiktk12328-Mar-12 21:32 
QuestionRegarding session variable usage in asp.net web apps Pin
Tridip Bhattacharjee28-Mar-12 20:14
professionalTridip Bhattacharjee28-Mar-12 20:14 
GeneralMy vote of 5 Pin
Sandeepkumar Ramani26-Mar-12 20:46
MemberSandeepkumar Ramani26-Mar-12 20:46 
GeneralMy vote of 5 Pin
Monjurul Habib26-Dec-11 6:45
professionalMonjurul Habib26-Dec-11 6:45 
Generalmy vote of 5 Pin
Uday P.Singh4-Dec-11 18:59
MemberUday P.Singh4-Dec-11 18:59 
QuestionMy 5+ Pin
Tejas Vaishnav4-Nov-11 21:05
professionalTejas Vaishnav4-Nov-11 21:05 
GeneralMy vote of 5 Pin
Sandesh M Patil14-Jun-11 0:44
MemberSandesh M Patil14-Jun-11 0:44 
GeneralMy vote of 5 Pin
Filip D'haene30-May-11 6:30
MemberFilip D'haene30-May-11 6:30 
GeneralNice but I have couple of questions Pin
Ankur\m/26-May-11 23:53
professionalAnkur\m/26-May-11 23:53 
GeneralCool stuff Pin
Blue_Boy26-May-11 23:01
MemberBlue_Boy26-May-11 23:01 
GeneralMy vote of 5 Pin
thatraja26-May-11 15:54
professionalthatraja26-May-11 15:54 
GeneralRe: My vote of 5 Pin
Abhijit Jana26-May-11 17:39
professionalAbhijit Jana26-May-11 17:39 
GeneralRe: My vote of 5 Pin
thatraja23-Jun-11 20:36
professionalthatraja23-Jun-11 20:36 
GeneralRe: My vote of 5 Pin
Abhijit Jana23-Jun-11 22:24
professionalAbhijit Jana23-Jun-11 22:24 
GeneralGood advice as usual Abhijit Pin
Sacha Barber26-May-11 5:32
mvaSacha Barber26-May-11 5:32 
GeneralRe: Good advice as usual Abhijit Pin
Abhijit Jana26-May-11 5:38
professionalAbhijit Jana26-May-11 5:38 
GeneralRe: Good advice as usual Abhijit Pin
Sacha Barber26-May-11 6:24
mvaSacha Barber26-May-11 6:24 
GeneralRe: Good advice as usual Abhijit Pin
Abhijit Jana26-May-11 8:04
professionalAbhijit Jana26-May-11 8:04 
GeneralRe: Good advice as usual Abhijit Pin
Sacha Barber26-May-11 9:22
mvaSacha Barber26-May-11 9:22 
GeneralRe: Good advice as usual Abhijit Pin
Sacha Barber26-May-11 9:26
mvaSacha Barber26-May-11 9:26 
GeneralMy vote of 5 Pin
Abhishek Sur25-May-11 22:40
professionalAbhishek Sur25-May-11 22:40 
GeneralMy vote of 5 Pin
Kunal Chowdhury «IN»25-May-11 21:59
professionalKunal Chowdhury «IN»25-May-11 21:59 
GeneralRe: My vote of 5 Pin
Abhijit Jana25-May-11 22:36
professionalAbhijit Jana25-May-11 22:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.