13

How do I store a variable or object application wide in ASP.net MVC?

In regular ASP, you had the Application object and in ASP.net too apparently.

I'm using ASP.net MVC 2.

1 Answer 1

23

In a controller you should be able to do this:

this.HttpContext.Application["foo"] = "bar";
Sign up to request clarification or add additional context in comments.

3 Comments

For testability I'd be inclined to interface out this call to remove the dependency on HttpContext within the controller.
ASP.NET MVC relies on ASP.NET. so you can use almost any ASP.NET Feature.
Beware for race conditions, you should hold a lock while reading or writing such global state. Multiple requests can be handled concurrently by different threads.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.