views:

97

answers:

4

I'm familiar with the Application and Session Key/Value object stores in ASP.NET. I'm looking for a global store that I can put a value into when a request is made that is valid only for the duration of the request but is also accessible to the DLL's in the business layer. I've been told that there's an HttpRequest object that does this but haven't been able to find further info on it.

The use case would be the click of a button on a web page causing a POST to the server and I would then create a value that I would want to stuff into memory just for the duration of the request until I returned the HTML to the client.

+1  A: 

You could add your items to HttpContext.Current.Items.

For more information I would recommend that you read 3 hot uses for HttpContext.Current.Items THEY Won't tell you about

Generally, HttpContext.Current.Items doesn’t get all that much hot blog press, but let me tell you, I’m here to change all that. For those out of the know, System.Web.HttpContext.Current.Items is a sweet key-value pair collection you can use to pass objects around up and through all components that participate in a single HTTP request.

Andrew Hare
+1  A: 

I believe you're looking for the Page.Context property, specifically the Items collection within it.

richardtallent
+1  A: 

You need to use HttpContext.Current.Items:

 HttpContext.Current.Items.Add("key", "value");
 string val =  HttpContext.Current.Items["key"] as string;

More info

Also there are issues with storing objects in thread context and CallContext in an asp.net application. The only safe option for maintaining a object for the duration of the request seems to HttpContext.Current.Items

Ngm
+1  A: 

4GuysFromRolla has a nice article about using the HttpContext to store items per request.

Vinz