带有列表的 asp.net 核心 ioptions
Posted
技术标签:
【中文标题】带有列表的 asp.net 核心 ioptions【英文标题】:asp.net core ioptions with a list 【发布时间】:2017-08-01 06:11:20 【问题描述】:我正在尝试从appsettings.json
文件中读取值列表。我可以毫无问题地读取Logging
值,但列表值(即Servers
)为空:
appsettings.json:
"Logging":
"IncludeScopes": false,
"LogLevel":
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
,
"Servers": [
"SCHVW2K12R2-DB",
"SCHVW2K12R2-DB\\MSSQL2016",
"SCHVW2K8R2-DB"
]
对象类:
public class AppSettingsConfiguration
public Logging Logging get; set;
public Servers Servers get; set;
//Logging Objects
public class Logging
public bool IncludeScopes get; set;
public LogLevel LogLevel get; set;
public class LogLevel
public string Default get; set;
public string System get; set;
public string Microsoft get; set;
//Server Objects
public class Servers
public List<string> Names get; set;
【问题讨论】:
【参考方案1】:尝试删除Servers
类并将AppSettingsConfiguration
更改为:
public class AppSettingsConfiguration
public Logging Logging get; set;
public string[] Servers get; set;
Servers
是一个简单的字符串数组,不是复杂类型。
【讨论】:
不客气!请看***.com/help/someone-answers【参考方案2】:要使用你原来的类,你可以把你的 json 改成这个。 IMO,这更具可读性。
"Logging":
"IncludeScopes": false,
"LogLevel":
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
,
"Servers":
"Names": [
"SCHVW2K12R2-DB",
"SCHVW2K12R2-DB\\MSSQL2016",
"SCHVW2K8R2-DB"
]
【讨论】:
以上是关于带有列表的 asp.net 核心 ioptions的主要内容,如果未能解决你的问题,请参考以下文章
ASP.NET Core 读取配置 IOptions 控制器未触发
当我在 asp.net 核心中选择下拉值时,如何将特定文本设置为下拉列表?