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?
Try using the WebConfigurationManager class instead. For example:
I found this solution to be quite helpful. It uses C# 4.0 DynamicObject to wrap the ConfigurationManager. So instead of accessing values like this:
you access them as a property:
EDIT: Adding code snippet in case link becomes stale:
This issue happens if this project is being used by another project. Make sure you copy the app setting keys to the parent project's app.config or web.config.
with assuming below setting in .config file:
try this: