Reading a key from the Web.Config using ConfigurationManager
C#asp.net MvcC# Problem Overview
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?
C# Solutions
Solution 1 - C#
Try using the WebConfigurationManager class instead. For example:
string userName = WebConfigurationManager.AppSettings["PFUserName"]
Solution 2 - C#
var url = ConfigurationManager.AppSettings["ServiceProviderUrl"];
Solution 3 - C#
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:
WebConfigurationManager.AppSettings["PFUserName"]
you access them as a property:
dynamic appSettings = new AppSettingsWrapper();
Console.WriteLine(appSettings.PFUserName);
EDIT: Adding code snippet in case link becomes stale:
public class AppSettingsWrapper : DynamicObject
{
private NameValueCollection _items;
public AppSettingsWrapper()
{
_items = ConfigurationManager.AppSettings;
}
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
result = _items[binder.Name];
return result != null;
}
}
Solution 4 - C#
If the caller is another project, you should write the config in caller project not the called one.
Solution 5 - C#
Full Path for it is
System.Configuration.ConfigurationManager.AppSettings["KeyName"]
Solution 6 - C#
There will be two Web.config files. I think you may have confused with those two files.
Check this image:
In this image you can see two Web.config files. You should add your constants to the one which is in the project folder not in the views folder
Hope this may help you
Solution 7 - C#
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.
Solution 8 - C#
Also you can try this line to get string value from app.config
file.
var strName= ConfigurationManager.AppSettings["stringName"];
Solution 9 - C#
with assuming below setting in .config file:
<configuration>
<appSettings>
<add key="PFUserName" value="myusername"/>
<add key="PFPassWord" value="mypassword"/>
</appSettings>
</configuration>
try this:
public class myController : Controller
{
NameValueCollection myKeys = ConfigurationManager.AppSettings;
public void MyMethod()
{
var myUsername = myKeys["PFUserName"];
var myPassword = myKeys["PFPassWord"];
}
}
Solution 10 - C#
Sorry I've not tested this but I think it's done like this:
var filemap = new System.Configuration.ExeConfigurationFileMap();
System.Configuration.Configuration config = System.Configuration.ConfigurationManager.OpenMappedExeConfiguration(filemap, System.Configuration.ConfigurationUserLevel.None);
//usage: config.AppSettings["xxx"]