【发布时间】:2014-03-14 21:39:53
【问题描述】:
在 App_Code 文件夹中,我创建了一个新类,我想在其中设置应用程序状态变量的值。
为了从应用程序状态读取,我使用了以下代码:
string text = (string)HttpContext.Current.Application["text"];
但现在我想设置应用程序状态的值。我尝试了以下代码,但没有成功:
System.Web.HttpApplicationState.Application["text"] = "string";
设置应用程序状态变量值的正确方法是什么?
【问题讨论】:
-
你怎么知道它不起作用?
-
Visual Studio 用红色下划线“Application”,并表示:“'System.Web.HttpApplicationState' 不包含'Application'的定义”
-
如果我没看错你的问题,你为什么不在你想写的时候用同样的阅读方法呢?
HttpContext.Current.Session["text"] = YOUR_VALUE; -
据我所知,这种方法仅供阅读。你确定它会起作用吗? Visual Studio 似乎接受它。
-
@MohammedElSayed 提到了正确的事情,您应该使用两者中的任何一个进行读写。
标签: c# asp.net class global-variables application-state