Dan*_*eck 41 .net asp.net configuration
我试图用来ConfigurationManager.AppSettings.GetValues()检索单个键的多个配置值,但我总是收到一个只有最后一个值的数组.我的appsettings.config样子
<add key="mykey" value="A"/>
<add key="mykey" value="B"/>
<add key="mykey" value="C"/>
Run Code Online (Sandbox Code Playgroud)
而我正试图访问
ConfigurationManager.AppSettings.GetValues("mykey");
Run Code Online (Sandbox Code Playgroud)
但我只是得到了{ "C" }.
关于如何解决这个问题的任何想法?
Joe*_*oel 46
尝试
<add key="mykey" value="A,B,C"/>
Run Code Online (Sandbox Code Playgroud)
和
string[] mykey = ConfigurationManager.AppSettings["mykey"].Split(',');
Run Code Online (Sandbox Code Playgroud)
Sql*_*yan 11
配置文件将每一行视为赋值,这就是为什么你只看到最后一行.当它读取配置时,它会为你的键分配"A"的值,然后是"B",然后是"C",并且由于"C"是最后一个值,它就是那个坚持的值.
正如@Kevin建议的那样,执行此操作的最佳方法可能是一个值,其内容为CSV,您可以将其拆分.
我知道我迟到了但是我找到了这个解决方案,它完美无缺,所以我只想分享.
这都是关于定义自己的 ConfigurationElement
namespace Configuration.Helpers
{
public class ValueElement : ConfigurationElement
{
[ConfigurationProperty("name", IsKey = true, IsRequired = true)]
public string Name
{
get { return (string) this["name"]; }
}
}
public class ValueElementCollection : ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new ValueElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((ValueElement)element).Name;
}
}
public class MultipleValuesSection : ConfigurationSection
{
[ConfigurationProperty("Values")]
public ValueElementCollection Values
{
get { return (ValueElementCollection)this["Values"]; }
}
}
}
Run Code Online (Sandbox Code Playgroud)
在app.config中只需使用您的新部分:
<configSections>
<section name="PreRequest" type="Configuration.Helpers.MultipleValuesSection,
Configuration.Helpers" requirePermission="false" />
</configSections>
<PreRequest>
<Values>
<add name="C++"/>
<add name="Some Application"/>
</Values>
</PreRequest>
Run Code Online (Sandbox Code Playgroud)
当像这样检索数据时:
var section = (MultipleValuesSection) ConfigurationManager.GetSection("PreRequest");
var applications = (from object value in section.Values
select ((ValueElement)value).Name)
.ToList();
Run Code Online (Sandbox Code Playgroud)
最后感谢原帖的作者
你想做什么是不可能的.您必须以不同方式命名每个键,或者执行value ="A,B,C"之类的操作,并将代码中的不同值分开string values = value.split(',').
它将始终获取最后定义的键的值(在您的示例C中).
小智 5
我使用键的命名约定,它就像一个魅力
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="section1" type="System.Configuration.NameValueSectionHandler"/>
</configSections>
<section1>
<add key="keyname1" value="value1"/>
<add key="keyname21" value="value21"/>
<add key="keyname22" value="value22"/>
</section1>
</configuration>
Run Code Online (Sandbox Code Playgroud)
var section1 = ConfigurationManager.GetSection("section1") as NameValueCollection;
for (int i = 0; i < section1.AllKeys.Length; i++)
{
//if you define the key is unique then use == operator
if (section1.AllKeys[i] == "keyName1")
{
// process keyName1
}
// if you define the key as a list, starting with the same name, then use string StartWith function
if (section1.AllKeys[i].Startwith("keyName2"))
{
// AllKeys start with keyName2 will be processed here
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
54399 次 |
| 最近记录: |