Can't read appSettings value from Web.Config

左心房为你撑大大i 提交于 2020-01-23 07:02:10

问题


I have the following in my web.config:

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

I have the following code in my codebehind:

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

However, when I look at webConfig, webConfig.AppSettings.Settings.Count = 0 .

Why is it not reading the app setting?

What I want to do is be able to get the setting by using:

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

I am using c# 3.5, vs 2008


回答1:


Why don't you just write this ?

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



回答2:


try this :

ConfigurationManager.AppSettings["PsychMon"];

or ( for global)

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



回答3:


Instead of creating the webConfig variable, why not just use ConfigurationManager.AppSettings["PsychMon"]



来源:https://stackoverflow.com/questions/11651005/cant-read-appsettings-value-from-web-config

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!