无法从Web.Config读取appSettings值

6

我在我的 web.config 文件中有以下内容:

<configuration>
    <appSettings>
        <add key="PsychMon" value="true"/>
    </appSettings>
 . . .
</configuration>

我在我的代码后台中有以下代码:

  System.Configuration.Configuration webConfig = 
System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(null) ; 

然而,当我查看webConfig时,webConfig.AppSettings.Settings.Count = 0。

为什么它没有读取应用程序设置?

我的目标是通过以下方式获取设置:

          System.Configuration.KeyValueConfigurationElement psych = 
webConfig.AppSettings.Settings["PsychMon"];

我正在使用c# 3.5和vs 2008。

1
你确定你正在访问正确的 web.config 文件吗? - Piotr Justyna
3个回答

13

为什么不直接写这样?

string value = 
    System.Web.Configuration.WebConfigurationManager.AppSettings["PsychMon"];

3
尝试这个:
ConfigurationManager.AppSettings["PsychMon"];

或者(全局)
 Configuration webConfig = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);

1

为什么不直接使用ConfigurationManager.AppSettings["PsychMon"],而要创建webConfig变量呢?


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接