从页面外部访问应用程序状态
我正在尝试从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我想我明白了!
I think I got it!
根据设计,无法从 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.尝试
Try
您可以简单地创建一个静态类成员:
这会存储在应用程序级别,这意味着您可以获得与应用程序状态相同的功能。静态成员将在所有层(网页和非网页)上可用。
You can simply create a static class member:
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).