Access Application State from outside a Page
Access Application State from outside a Page
I'm trying to access the Application State from a utility class in the web application but I'm not sure of the best way to do it. I looked for a static member but there is none of use (I was thinking somewhere along the lines of HttpContext.Current).
The best solution I found so far is to have a member in the utility class that will be initialized in Application_Start event of the Global.asax.cs (I can get it from this.Application there) but would there be any risk of "something" happening to that reference (I don't care about the application being restarted as I'm only looking for a cache functionality)?
4 Answers
4
I think I got it!
HttpContext.Current.Application
Works for me too. Thanks. @CyberDude: please consider making this the accepted response.
– Chris
Mar 14 '13 at 16:02
Just double "p" in "Application:) It doesn't allow me to edit the answer just adding one character.
– Daniil Veriga
Mar 16 at 10:29
Fixed! Interesting how 1500 people never caught that...
– CyberDude
Mar 22 at 19:39
There is no way to access Application state from outside the HTTTPContext by design. Using Application_Start is the preferred way to initialize "global" values. You can also use the Cache
class for the same purpose. Cache
offers member expiration features that can be useful for data that changes due to events such as a file or database value changing.
Cache
Cache
Yes, Cache has more functionality, I might use it instead.
– CyberDude
Sep 2 '10 at 18:09
Try
System.Web.HttpRuntime
You can simply create a static class member:
public class MyGlobalCache
public static string SomeValueget;set;
This gets stored at the application level, which means that you get the same functionality of the Application state. The static member will be available across all layers (webpages and non-webpages).
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
It works man, cheers.
– TheGateKeeper
Nov 20 '12 at 19:02