24

We using ASP.NET 3.5 (Controls-based approach) and need to have storage specific for one http request only.

Thread-specific cache with keys from session id won't work because threads are supposed to be pooled and therefore I have a chance to have data from some previous request in cache, which is undesirable in my case. I always need to have brand new storage for each request available through whole request.

Any ideas how to do it in ASP.NET 3.5?

Artem
  • 7,275
  • 15
  • 57
  • 97

3 Answers3

39

We have used HttpContext.Current.Items collection to do RequestScope caching. It works well.

Valera Kolupaev
  • 2,285
  • 14
  • 14
  • Important- using await or using multiple threads can cause HttpContext.Current to be different or null. – Pangamma Feb 02 '21 at 02:11
6

just to clarify what ggonsalv was referring to

http://msdn.microsoft.com/en-us/library/system.web.httpcontext.items.aspx

HttpContext.Items["key"] = value;

UPDATE: the mvc specific version

http://msdn.microsoft.com/en-us/library/system.web.mvc.controller.httpcontext(v=VS.100).aspx

house9
  • 20,359
  • 8
  • 55
  • 61
2

How about using the Context collection. This allows data to be shared between all your controls but only lasts for the request.

Use it like this context.Items("base_url") = "default.aspx"

ggonsalv
  • 1,264
  • 8
  • 18
  • Duh. Got a call.. but house9 got it on the **head** HttpContext.Items["key"] = value; or context.Items("base_url") = "default.aspx" – ggonsalv Apr 27 '10 at 18:59