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";
3
  • 2
    For testability I'd be inclined to interface out this call to remove the dependency on HttpContext within the controller. Aug 7, 2010 at 14:58
  • 1
    ASP.NET MVC relies on ASP.NET. so you can use almost any ASP.NET Feature.
    – Arthur
    Aug 7, 2010 at 15:15
  • 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.
    – Wim Coenen
    May 8, 2012 at 13:13

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.