-1

I am facing weird issue in reading appSetting from Web.Config I always get null.

Why so?

Web.config

<!-- Message setting-->
<add key="applicationType" value="release"/>

Code:

string applicationType = ConfigurationManager.AppSettings["applicationType"];

worth to mention here

Web.config is part of another project and i am trying to access it from another project,

Rahul Nikate
  • 6,192
  • 5
  • 42
  • 54
TheKingPinMirza
  • 7,924
  • 6
  • 51
  • 81

3 Answers3

1

Web.config must be a part of project has been started.

enter image description here

If you have started BotServiceHost, the ConfigurationManager will be using config inside BotServiceHost. But for example if you started from console app(sandbox) same dll(BotServiceHost), you must add another config file inside this sandbox!

It's doesn't matter what are you using app.config or web.config

isxaker
  • 8,446
  • 12
  • 60
  • 87
0

worth to mention here

Web.config is part of another project and i am trying to access it from another project,

That's your issue.

ConfigurationManager.AppSettings uses the configuration file tied to your entry assembly, you can't use one from a different project.

In this particular instance, your web.config should be a part of your project, in the root of the application

Alex
  • 37,502
  • 51
  • 204
  • 332
0

If Your application contains multiple Visual Studio Projects, you need to make sure you are accessing key with same project and not from another project in your solution.

If you key belongs to another project in your solution then Here's the code to do the same.

ConfigurationFileMap fileMap = new ConfigurationFileMap(file); //Path to your config file
Configuration configuration = ConfigurationManager.OpenMappedMachineConfiguration(fileMap);
string applicationType = configuration.AppSettings.Settings["applicationType"].Value;

Please follow below link for more information.
ConfigurationManager.OpenExeConfiguration Method (String)

Rahul Nikate
  • 6,192
  • 5
  • 42
  • 54