Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading a key from the Web.Config using ConfigurationManager

Tags:

c#

asp.net-mvc

I am trying to read the keys from the Web.config file in a different layer than the web layer (Same solution)

Here is what I am trying:

string userName = System.Configuration.ConfigurationManager.AppSettings["PFUserName"]; string password = System.Configuration.ConfigurationManager.AppSettings["PFPassWord"]; 

And here is my appSettings in the Web.config file:

<configuration>    ....    <appSettings>       <add key="PFUserName" value="myusername"/>       <add key="PFPassWord" value="mypassword"/>    </appSettings>    .... </configuration> 

When I debug the code username and password are just null, so it is not getting the value of the keys.

What am I doing wrong to read these values?

like image 650
twal Avatar asked Jan 04 '11 15:01

twal


People also ask

What is the use of ConfigurationManager in C#?

The ConfigurationManager class enables you to access machine, application, and user configuration information. This class replaces the ConfigurationSettings class, which is deprecated. For web applications, use the WebConfigurationManager class.

What is ConfigurationManager AppSettings?

it is a .net builtin mechanism to define some settings before the application starts, without recompiling. see msdn configurationmanager.


1 Answers

Try using the WebConfigurationManager class instead. For example:

string userName = WebConfigurationManager.AppSettings["PFUserName"] 
like image 92
Hector Correa Avatar answered Sep 20 '22 06:09

Hector Correa