C# 使用ConfigurationManager从Web.Config读取密钥
我试图从C# 使用ConfigurationManager从Web.Config读取密钥,c#,asp.net-mvc,C#,Asp.net Mvc,我试图从Web.config文件中读取与Web层不同的层中的密钥(相同的解决方案) 以下是我正在尝试的: string userName = System.Configuration.ConfigurationManager.AppSettings["PFUserName"]; string password = System.Configuration.ConfigurationManager.AppSettings["PFPassWord"]; 这是我在Web.config文件中的appS
Web.config
文件中读取与Web层不同的层中的密钥(相同的解决方案)
以下是我正在尝试的:
string userName = System.Configuration.ConfigurationManager.AppSettings["PFUserName"];
string password = System.Configuration.ConfigurationManager.AppSettings["PFPassWord"];
这是我在Web.config
文件中的appSettings
:
<configuration>
....
<appSettings>
<add key="PFUserName" value="myusername"/>
<add key="PFPassWord" value="mypassword"/>
</appSettings>
....
</configuration>
....
....
当我调试代码时,username
和password
只是null
,因此它没有得到密钥的值
读取这些值时我做错了什么?尝试改用WebConfiguration Manager类。例如:
string userName = WebConfigurationManager.AppSettings["PFUserName"]
对不起,我没有测试过,但我认为它是这样做的:
var filemap = new System.Configuration.ExeConfigurationFileMap();
System.Configuration.Configuration config = System.Configuration.ConfigurationManager.OpenMappedExeConfiguration(filemap, System.Configuration.ConfigurationUserLevel.None);
//usage: config.AppSettings["xxx"]
WebConfigurationManager.AppSettings["PFUserName"]
如果此项目正被其他项目使用,则会发生此问题。确保将应用程序设置键复制到父项目的app.config或web.config 。它使用C#4.0 DynamicObject包装ConfigurationManager。因此,不要像这样访问值:
var filemap = new System.Configuration.ExeConfigurationFileMap();
System.Configuration.Configuration config = System.Configuration.ConfigurationManager.OpenMappedExeConfiguration(filemap, System.Configuration.ConfigurationUserLevel.None);
//usage: config.AppSettings["xxx"]
WebConfigurationManager.AppSettings["PFUserName"]
您可以将其作为属性访问:
dynamic appSettings = new AppSettingsWrapper();
Console.WriteLine(appSettings.PFUserName);
编辑:在链接过时时添加代码段:
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;
}
}
如果调用者是另一个项目,则应在调用者项目而不是被调用者项目中写入配置。此外,您可以尝试此行从
app.config
文件中获取字符串值
var strName= ConfigurationManager.AppSettings["stringName"];
它的完整路径是
System.Configuration.ConfigurationManager.AppSettings["KeyName"]
将有两个Web.config文件。我想你可能把那两份文件弄混了 检查此图像: 在此图像中,您可以看到两个Web.config文件。您应该将常量添加到项目文件夹中而不是视图文件夹中的常量中
希望这可以帮助您在.config文件中设置以下内容:
<configuration>
<appSettings>
<add key="PFUserName" value="myusername"/>
<add key="PFPassWord" value="mypassword"/>
</appSettings>
</configuration>
谢谢你的回答,我按照你的建议做了尝试,但仍然得到了同样的结果。实际上,我现在在ToString()上得到了一个NullReferenceException,如果你得到了一个null异常,这意味着没有找到设置。尝试这样做:“object x=WebConfigurationManager.AppSettings[“PFUserName”];”,您可能会得到一个空值,这意味着它找不到请求的设置。您确定设置位于正确的web.config上吗?(我以前犯过一个错误,在web.config的“视图”下转储了我的值)文件夹,并浪费了大量时间来解决它为什么不能按预期工作的问题。谢谢,您可以删除ToString,因为AppSettings indexer已经返回string。我认为不需要显式调用ToString。括号中的返回类型已经是string了,不是吗?在我看来像“string”。ToString()-虽然技术上正确,但这有点多余。您的网站如何访问第二个项目?您的语法是正确的。您可能编辑了错误的web.config文件,这就是它返回
NULL
的原因。后期评论,但没有人指出这一点。这就是发生在我身上的事情,我在Views web.config中。只有web项目可以访问System.Configuration.ConfigurationManager.AppSettings对象。其他层无法访问此对象,因为它们未实现System.Web.Right answer。ToString()
是多余的,但是。如果值不在应用程序配置中,则ToString可能是危险的。最好捕获返回的对象,并在解析之前测试null。如果配置中没有“mysettings”,则尝试运行它,您将看到异常弹出。相反,下面的内容可能更安全……string key=“mysettings”;string value=ConfigurationManager.AppSettings[key];如果(value==null)value=“unknown value”;则不需要.ToString(),因为它是string@JoeHealy您可以使用空合并运算符缩短该值并提高清晰度:string value=ConfigurationManager.AppSettings[key]??“未知值”
我想知道这个答案和OP问题中的代码有什么区别!它们不一样吗?