2012-07-25 53 views
3

的appSettings值我在我的web.config以下:无法读取的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

回答

7

你为什么不写这个?

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

试试这个:

ConfigurationManager.AppSettings["PsychMon"]; 

或(全球)

Configuration webConfig = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath); 
0

Perh aps您正在使用转换修改您的Web配置的生产版本。

0

您确定您正在访问正确的web.config吗?

1

而不是创建webConfig变量,为什么不使用ConfigurationManager.AppSettings["PsychMon"]