Click here to Skip to main content
15,880,796 members
Articles / Web Development / ASP.NET

When Can We Use HttpContext.Current.Items to Store Data in ASP.NET?

Rate me:
Please Sign up or sign in to vote.
4.96/5 (27 votes)
14 Jan 2011CPOL2 min read 162.6K   28   9
When can we use HttpContext.Current.Items to stores data in ASP.NET?

To answer this question in a single statement, you can use HttpContext.Current.Items for very short term storage. By Short term storage, we mean that this data is valid for a single HTTP Request. There is a lot of confusion around regarding storing data in HttpContext.Current.Items and storing data in Session variable. In this blog post, I am going to describe what are the different scenarios where we can use HttpContext.Current.Items and what is the exact difference with session variable.

Items collections of HttpContext is and IDictionary key-value collections and those are shared across a single HTTPRequest. Yes, HttpContext.Current.Items is valid for a single HTTPRequest. Once after processing, server information is sent back to the browser, the variables that were set in the Items[] collection will be lost. Whereas for Session variable, the information is valid for multiple requests as this is user specific. The session variable only expires either on Session Time Out or explicitly clears the values.

Let’s have a quick look at how we can store information in HttpContext.Current.Items:

C#
HttpContext.Current.Items["ModuleInfo"] = "Custom Module Info"

And retrieve it like:

C#
string contextData = (string)(HttpContext.Current.Items["ModuleInfo"]);

As I said, HttpContext.Current.Items stores data for a very limited time period, then when can we use this? Yes, this is extremely useful when we want to share content between HttpModule and HTTPHandler.

image

Because each and every client request passes through the HTTP Pipeline and HTTP Pipeline consists of HTTP Module and HTTP Handler. So If you are writing one custom HTTP Module by Implementing IHttpModule and you want pass some information from this module to the current requested page or any other module, you can use the HttpContext.Current.Items to store the data.

image

Similarly, you use HTTPContext Items collection when you are sharing the same information across the different instance based on the user request and that request could be changed for a different request.

While using Context Items collections you need to keep one things that, Items collections holds the objects, so you need to do proper type casting while retrieving it.

To summarize, in ASP.NET, HttpContext.Current.Items allows us to hold information for a single request. We can use it to store short term information. Storing such kind of information is extremely helpful to send information across different custom modules or to requested pages. You have to make sure that the data you are using in HttpContext.Current.Items is only valid for that current request and data should be flushed out automatically when request is sent to a browser for any new request you have to store the data again. Whereas session variable is valid for every request unless session timeout is not reached or we explicitly clear the session.

Hope this will help!

Thanks!

AJ

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

License

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


Written By
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

 
QuestionGenerate DBContext can use HttpContext.Current.Items Pin
Member 1306455816-Mar-17 17:53
Member 1306455816-Mar-17 17:53 
GeneralMy vote of 2 Pin
hello988-Sep-14 23:32
hello988-Sep-14 23:32 
GeneralMy vote of 5 Pin
Snesh Prajapati7-Apr-14 21:25
professionalSnesh Prajapati7-Apr-14 21:25 
QuestionMy vote for 5 Pin
kulkarni.ajay6-Feb-13 1:33
kulkarni.ajay6-Feb-13 1:33 
GeneralMy vote of 5 Pin
kulkarni.ajay6-Feb-13 1:31
kulkarni.ajay6-Feb-13 1:31 
QuestionDifference between both Pin
CIAP22-Jan-13 8:05
CIAP22-Jan-13 8:05 
Clear like a water Abhijit Jana!. Tanks a lot. Regards from Argentina
GeneralMy vote of 5 Pin
thatraja15-Jan-11 18:52
professionalthatraja15-Jan-11 18:52 
GeneralMy vote of 5 Pin
Brij15-Jan-11 4:01
mentorBrij15-Jan-11 4:01 
GeneralMy vote of 5 Pin
Abhishek Sur14-Jan-11 22:23
professionalAbhishek Sur14-Jan-11 22:23 

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.