3

In HttpContext(Or some thing like this) I need to add a temperory variable from a controller which need to available through out the request processing(Request wise variable). But the HttpContext.Current.Request is readonly. If i'm adding in Items its not getting outside. How can i achieve this

Thanks & Regards Binesh Nambiar C

1 Answer 1

7

You are looking for HttpContext.Items, which is a dictionary that can be used to store items for the duration of the current request. It goes out of scope at the end of the request.

// Set HttpContext.Items["Customer"] = customer; // Get var customer = HttpContext.Items["Customer"]; 
Sign up to request clarification or add additional context in comments.

4 Comments

I tried HttpContext.Current.Items. Its not working. I'm using the field now in a custom modelbinder
Could you provide more details what you are trying to achieve? (please edit your question and add them). Model binding happens near the beginning of the request, so it probably doesn't make much sense to get values from the request cache (although it might be a good place to set them).
BTW - You should never use the static HttpContext.Current in MVC (which might be your problem). In a model binder, the context object is passed in. You can access the request cache like var cache = controllerContext.HttpContext.Items["Key"];
Hi I Solved by adding to header of Request proprty

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.