jump to navigation

When we can use HttpContext.Current.Items to stores data in ASP.NET? January 14, 2011

Posted by Abhijit Jana in ASP.NET, ASP.NET 4.0, Tips and Tricks, Visual Studio.
Tags: , , ,
trackback

To answer this question In a single statement, you can use HttpContext.Current.Items for very short term storage. By Short term storage means, this data is valid for a single HTTP Request.  There are many 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 that are shared across a single HTTPRequest. Yes, HttpContext.Current.Items  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 lost. Where as for Session Variable, information valid for multiple request as this is user specific. The session variable only  expires either on Session Time Out or explicitly clear the values.

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

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

And retrieve it like

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

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

image

Because each and every client requests pass 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 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 same information across the different instance based on the user request and that request could be change for 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 an single request. We can use it to store short term information. Storing such kind information 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 flashed out automatically when request sent to browser for any new request you have to store the data again. Where as session variable is valid for every request unless session timeout not reached or we are explicitly clear the session.

Hope this will help !

Thanks !

AJ

Comments»

1. Tweets that mention When we can use HttpContext.Current.Items to stores data in ASP.NET? « Abhijit's World of .NET -- Topsy.com - November 30, 1999

[...] This post was mentioned on Twitter by Kunal Chowdhury. Kunal Chowdhury said: RT @AbhijitJana: [New Blog Post ] When we can use HttpContext.Current.Items to stores data in asp.net : http://wp.me/ppvPE-zE #aspne … [...]

2. Dew Drop – January 14, 2011 | Alvin Ashcraft's Morning Dew - November 30, 1999

[...] When we can use HttpContext.Current.Items to stores data in ASP.NET? (Abhijit Jana) [...]

3. When we can use HttpContext.Current.Items to stores data in ASP.NET? - January 14, 2011

[...] is valid for a single HTTP Request. There are many confusion around regarding storing data in… [full post] Abhijit Jana Abhijit's World of .NET asp.netasp.net 4.0tips and tricksvisual studio [...]

4. Few important tips that you should know while using ASP.NET Session « Abhijit's World of .NET - May 3, 2011

[...] is one of my complete blog post When we can use HttpContext.Current.Items to stores data in ASP.NET?  Which describe the fundamentals of using HttpContext.Current.Items for storing short terms [...]

5. Few important tips that you should know while using ASP.NET Session - Daily .Net Tips - May 3, 2011

[...] is one of my complete blog post When we can use HttpContext.Current.Items to stores data in ASP.NET?  Which describe the fundamentals of using HttpContext.Current.Items for storing short terms [...]

6. Few important tips that you should know while using ASP.NET Session - Abhijit's Blog - May 3, 2011

[...] is one of my complete blog post When we can use HttpContext.Current.Items to stores data in ASP.NET?  Which describe the fundamentals of using HttpContext.Current.Items for storing short terms [...]

7. About HttpContext. If Current. - August 25, 2011
8. sravani - December 19, 2011

Nice explanation.. Thank you.

9. Vugar - July 27, 2012

Can we use HttpContext.Current.Items to send a string info from one asp.net project to another? Thanks


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 1,667 other followers

%d bloggers like this: