从页面外部访问应用程序状态

发布于 2024-09-17 11:16:33 字数 274 浏览 3 评论 0原文

我正在尝试从 Web 应用程序中的实用程序类访问应用程序状态,但我不确定最好的方法。我寻找了一个静态成员,但没有任何用处(我在思考 HttpContext.Current 的某个地方)。

到目前为止,我发现的最好的解决方案是在实用程序类中拥有一个成员,该成员将在 Global.asax.cs 的 Application_Start 事件中初始化(我可以从那里的 this.Application 获取它),但是会存在“某些东西”的风险吗? “发生在该引用上(我不关心应用程序是否重新启动,因为我只是在寻找缓存功能)?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

轻拂→两袖风尘 2024-09-24 11:16:34

我想我明白了!

HttpContext.Current.Application

I think I got it!

HttpContext.Current.Application
葬心 2024-09-24 11:16:34

根据设计,无法从 HTTPContext 外部访问应用程序状态。使用 Application_Start 是初始化“全局”值的首选方法。您还可以使用Cache 类来达到相同的目的。 Cache 提供成员过期功能,对于因文件或数据库值更改等事件而更改的数据非常有用。

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.

献世佛 2024-09-24 11:16:34

尝试

System.Web.HttpRuntime

Try

System.Web.HttpRuntime
开始看清了 2024-09-24 11:16:34

您可以简单地创建一个静态类成员:

public class MyGlobalCache
{
    public static string SomeValue{get;set;}
}

这会存储在应用程序级别,这意味着您可以获得与应用程序状态相同的功能。静态成员将在所有层(网页和非网页)上可用。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文