Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't read appSettings value from Web.Config

Tags:

c#

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

like image 993
Lill Lansey Avatar asked Jul 25 '12 13:07

Lill Lansey


People also ask

How do I use appSettings in web config?

The key/value pairs specified in the <appSettings> element are accessed in code using the ConfigurationSettings class. You can use the file attribute in the <appSettings> element of the Web. config and application configuration files.

Why appSettings keys in web config are not case sensitive?

Why appsettings keys (in web. config) are not case sensitive ? The default comparer is a CaseInsensitiveComparer that uses the conventions of the invariant culture; that is, key comparisons are case-insensitive by default.

What is IConfiguration C#?

The IConfiguration is an interface for . Net Core 2.0. The IConfiguration interface need to be injected as dependency in the Controller and then later used throughout the Controller. The IConfiguration interface is used to read Settings and Connection Strings from AppSettings. json file.


2 Answers

Why don't you just write this ?

string value = 
    System.Web.Configuration.WebConfigurationManager.AppSettings["PsychMon"];
like image 140
yogi Avatar answered Sep 30 '22 13:09

yogi


try this :

ConfigurationManager.AppSettings["PsychMon"];

or ( for global)

 Configuration webConfig = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
like image 24
Royi Namir Avatar answered Sep 30 '22 15:09

Royi Namir