我正在我的 appsettings.json
中存储一个键值对数组文件:
{
"ApplicationSettings": {
...
"CellularProviders": [
{
"Cingular": "cingularme.com"
},
{
"Nextel": "vtext.com"
},
{
"Sprint": "messaging.sprintpcs.com"
},
{
"T-Mobile": "tmomail.net"
},
{
"Verizon": "messaging.nextel.com"
},
{
"Virgin Mobile": "vmobl.com"
}
]
...
}
目前,我使用 List<Dictionary<string,string>>
在 Startup.cs
中填充的 POCO 类中:
public class ApplicationSettings
{
...
public List<Dictionary<string,string>> CellularProviders { get; set; }
}
这是表示键值对数组的推荐方式吗?
此外,我希望能够使用应用程序的“设置”部分在 Azure 中管理此设置:
在此上下文中表示键值对数组的正确方法是什么?
最佳答案
您无法放置JSON
进入应用程序设置,因为它是一组简单的键值对。为了覆盖您的 appsettings.json
中的设置您需要表示完整路径,按 :
分段s,作为键(值保持不变):
例如,一个 key
将是CellularProviders:Cingular
与 value
设置为cingularme.com
.
CellularProviders:Cingular = cingularme.com
CellularProviders:Nextel = vtext.com
CellularProviders:Sprint = messaging.sprintpcs.com
CellularProviders:T-Mobile = tmomail.net
CellularProviders:Verizon = messaging.nextel.com
CellularProviders:VirginMobile = vmobl.com
至于将其转换为 List<Dictionary<string, string>>
,我不确定我是否理解您为什么使用“词典列表”而不是 Dictionary<string, string>
。
一个Dictionary<TKey, TValue>
几乎是 List<T>
的KeyValuePair<TKey, TValue>
对象(注意,这有点过于笼统)。您应该考虑使用它:
public Dictionary<string,string> CellularProviders { get; set; }
这将允许您轻松地在集合中查找值,如下所示:
var providerValue = CellularProviders["Cingular"];
关于c# - 访问 IOption 类中 appsettings.json 文件中存储的键值对,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48611316/