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.Items
for 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.Items
andstoring
data in Session variable. In this blog post, I am going to describe what are the different scenarios where we can useHttpContext.Current.Items
and
what is the exact difference with session variable.
Items collections ofHttpContext
is andIDictionary
key-value
collections and that are shared across a singleHTTPRequest
. Yes,HttpContext.Current.Items
is
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 forSession
variable,
the information is valid for multiple requests as this is user specific. Thesession
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 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.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 betweenHttpModule
andHTTPHandler
.
Because each and every client request passes through theHTTP PipelineandHTTP Pipeline consistsofHTTP
ModuleandHTTP Handler. So If you are writing one custom HTTP Module by ImplementingIHttpModule
and
you want pass some information from this module to the current requested page or any other module, you can use theHttpContext.Current.Items
to
store the data.
Similarly, you useHTTPContext
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 propertype casting whileretrieving it.
To summarize, in ASP.NETHttpContext.Current.Items
allows 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 unlesssession
timeout
is not reached or we explicitly clear the session.
相關文章
- 架構之路(9):Session Per Request架構Session
- 架構之路(九)Session Per Request架構Session
- ASP.NET MVC got 405 error on HTTP DELETE requestASP.NETMVCGoErrorHTTPdelete
- 【es】FATAL [circuit_breaking_exception] [parent] Data too large, data for [<http_request>] would beUIExceptionHTTP
- [wget] HTTP request sent, awaiting response... No data received.wgetHTTPAI
- HTTP請求中的Form Data與Request Payload的區別HTTPORM
- Servlet&HTTP&RequestServletHTTP
- angularjs中$http模組傳送post請求request payload轉form dataAngularJSHTTPORM
- PHP Simulation HTTP Request(undone)PHPHTTP
- Jmeter之HTTP Request DefaultsJMeterHTTP
- HTTP Status 400 – Bad RequestHTTP
- asp.net 8 Request,Response,ServerASP.NETServer
- nginx: 400 Bad Request | The plain HTTP request was sent to HTTPS portNginxAIHTTP
- Error parsing HTTP request header--400 bad requestErrorHTTPHeader
- use azure data studio to create external table for oracleOracle
- Jmeter系列(21)- 詳解 HTTP RequestJMeterHTTP
- HTTP Headers的Request HeadersHTTPHeader
- Asp.Net Sync & ASync Processing requestASP.NET
- adc指令(Addition Carry)
- [ASP.NET] 使用Request 傳遞引數ASP.NET
- 基礎篇-http協議《http 簡介、url詳解、request》HTTP協議
- 2.3.1.1.3 Application Containers Use Case: Logical Data WarehouseAPPAI
- oracle ebs r12 use loadrunner load dataOracle
- undefinedindex:HTTP_RAW_POST_DATAUndefinedIndexHTTP
- nodejs使用request傳送http請求NodeJSHTTP
- ngx_http_discard_request_body 函式分析HTTP函式
- As per AIAI
- ASP.NET常見物件之Request與ResponseASP.NET物件
- ngx_http_request_t結構體詳解HTTP結構體
- http request-01-XMLHttpRequest XHR 簡單介紹HTTPXML
- Introduce to Data Storage Objects in ASP.NETObjectASP.NET
- Asp.Net Set Http TimeoutASP.NETHTTP
- Spring Mvc Http 400 Bad Request問題排查SpringMVCHTTP
- 使用 request 和 cheerio 庫來傳送 HTTP 請求HTTP
- http協議學習-請求頭Request HeadersHTTP協議Header
- HTTP請求中 request payload 和 formData 區別?HTTPORM
- $_POST,$HTTP_RAW_POST_DATA,php://inputHTTPPHP
- 關於 Angular HTTP Interceptor 中 Request 和 Response 的 immutable 特性AngularHTTP