Access Application State from outside a Page
Asked Answered
S

4

9

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)?

Seminole answered 2/9, 2010 at 17:37 Comment(0)
S
13

I think I got it!

HttpContext.Current.Application
Seminole answered 2/9, 2010 at 17:42 Comment(2)
Works for me too. Thanks. @CyberDude: please consider making this the accepted response.Nozzle
Just double "p" in "Application:) It doesn't allow me to edit the answer just adding one character.Starling
C
1

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.

Chi answered 2/9, 2010 at 17:46 Comment(1)
Yes, Cache has more functionality, I might use it instead.Seminole
F
0

Try

System.Web.HttpRuntime
Fictile answered 2/9, 2010 at 17:44 Comment(0)
O
0

You can simply create a static class member:

public class MyGlobalCache
{
    public static string SomeValue{get;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).

Opinionative answered 2/9, 2010 at 19:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.