Asp.net use HttpContext.Current.Items Carry data during per http request

Fromhttp://www.codeproject.com/Articles/146455/When-can-we-use-HttpContext-Current-Items-to-store



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

Items collections ofHttpContextis andIDictionarykey-value collections and that are shared across a singleHTTPRequest. Yes,HttpContext.Current.Itemsis valid for asingle HTTPRequest. Once after processing, server information is sent back to the browser, the variables that were set in theItems[]collection will be lost. Whereas forSessionvariable, the information is valid for multiple requests as this is user specific. Thesessionvariable only expires either on Session Time Out or explicitly clears the values.

Let’s have a quick look at how we can store information inHttpContext.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.Itemsstores data for a very limited time period, then when can we use this? Yes, this is extremely useful when we want to share content betweenHttpModuleandHTTPHandler.

image

Because each and every client request passes through theHTTP PipelineandHTTP Pipeline consistsofHTTP ModuleandHTTP Handler. So If you are writing one custom HTTP Module by ImplementingIHttpModuleand you want pass some information from this module to the current requested page or any other module, you can use theHttpContext.Current.Itemsto store the data.

image

Similarly, you useHTTPContextItems 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 propertype casting whileretrieving it.

To summarize, in ASP.NETHttpContext.Current.Itemsallows us to hold informationfor 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 inHttpContext.Current.Itemsis only valid for that current request and data should be flashed 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 unlesssessiontimeout is not reached or we explicitly clear the session.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值