Click here to Skip to main content
13,357,402 members (65,128 online)
Click here to Skip to main content
Add your own
alternative version


46 bookmarked
Posted 25 May 2011

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

, 25 May 2011
Rate this:
Please Sign up or sign in to vote.
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 :
Web Site :
Twitter : @AbhijitJana
My Kinect Book : Kinect for Windows SDK Programming Guide

You may also be interested in...


Comments and Discussions

GeneralMy vote of 5 Pin
shishpalsingh0124-Jul-13 20:22
membershishpalsingh0124-Jul-13 20:22 
GeneralMy vote of 5 Pin
Sanjay K. Gupta24-Apr-12 19:39
memberSanjay K. Gupta24-Apr-12 19:39 
GeneralMy vote of 5 Pin
kami528-Mar-12 22:32
memberkami528-Mar-12 22:32 
I found your articles very practical and useful. Thanks
QuestionRegarding session variable usage in web apps Pin
Tridip Bhattacharjee28-Mar-12 21:14
memberTridip Bhattacharjee28-Mar-12 21:14 
GeneralMy vote of 5 Pin
Sandeep Ramani26-Mar-12 21:46
memberSandeep Ramani26-Mar-12 21:46 
GeneralMy vote of 5 Pin
Monjurul Habib26-Dec-11 7:45
memberMonjurul Habib26-Dec-11 7:45 
Generalmy vote of 5 Pin
Uday P.Singh4-Dec-11 19:59
memberUday P.Singh4-Dec-11 19:59 
QuestionMy 5+ Pin
Tejas_Vaishnav4-Nov-11 22:05
memberTejas_Vaishnav4-Nov-11 22:05 
GeneralMy vote of 5 Pin
SChristmas14-Jun-11 1:44
memberSChristmas14-Jun-11 1:44 
GeneralMy vote of 5 Pin
Filip D'haene30-May-11 7:30
memberFilip D'haene30-May-11 7:30 
GeneralNice but I have couple of questions Pin
Ankur\m/27-May-11 0:53
memberAnkur\m/27-May-11 0:53 
GeneralCool stuff Pin
Blue_Boy27-May-11 0:01
memberBlue_Boy27-May-11 0:01 
GeneralMy vote of 5 Pin
thatraja26-May-11 16:54
mvpthatraja26-May-11 16:54 
GeneralRe: My vote of 5 Pin
Abhijit Jana26-May-11 18:39
mvpAbhijit Jana26-May-11 18:39 
GeneralRe: My vote of 5 Pin
thatraja23-Jun-11 21:36
mvpthatraja23-Jun-11 21:36 
GeneralRe: My vote of 5 Pin
Abhijit Jana23-Jun-11 23:24
mvpAbhijit Jana23-Jun-11 23:24 
GeneralGood advice as usual Abhijit Pin
Sacha Barber26-May-11 6:32
mvpSacha Barber26-May-11 6:32 
GeneralRe: Good advice as usual Abhijit Pin
Abhijit Jana26-May-11 6:38
mvpAbhijit Jana26-May-11 6:38 
GeneralRe: Good advice as usual Abhijit Pin
Sacha Barber26-May-11 7:24
mvpSacha Barber26-May-11 7:24 
GeneralRe: Good advice as usual Abhijit Pin
Abhijit Jana26-May-11 9:04
mvpAbhijit Jana26-May-11 9:04 
GeneralRe: Good advice as usual Abhijit Pin
Sacha Barber26-May-11 10:22
mvpSacha Barber26-May-11 10:22 
GeneralRe: Good advice as usual Abhijit Pin
Sacha Barber26-May-11 10:26
mvpSacha Barber26-May-11 10:26 
GeneralMy vote of 5 Pin
Abhishek Sur25-May-11 23:40
mvpAbhishek Sur25-May-11 23:40 
GeneralMy vote of 5 Pin
Kunal_Chowdhury25-May-11 22:59
mvpKunal_Chowdhury25-May-11 22:59 
GeneralRe: My vote of 5 Pin
Abhijit Jana25-May-11 23:36
mvpAbhijit Jana25-May-11 23: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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.180111.1 | Last Updated 26 May 2011
Article Copyright 2011 by Abhijit Jana
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid