Lill Lansey
Lill Lansey

Reputation: 4915

Can't read appSettings value from Web.Config

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

Upvotes: 6

Views: 10966

Answers (3)

ShaneBlake
ShaneBlake

Reputation: 11096

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

Upvotes: 1

yogi
yogi

Reputation: 19591

Why don't you just write this ?

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

Upvotes: 13

Royi Namir
Royi Namir

Reputation: 148514

try this :

ConfigurationManager.AppSettings["PsychMon"];

or ( for global)

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

Upvotes: 3

Related Questions