【问题标题】:asp.net core ioptions with a list带有列表的 asp.net 核心 ioptions
【发布时间】: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; }
}

【问题讨论】:

    标签: c# json asp.net-core


    【解决方案1】:

    尝试删除Servers 类并将AppSettingsConfiguration 更改为:

    public class AppSettingsConfiguration
    {
        public Logging Logging { get; set; }
        public string[] Servers { get; set; }
    }
    

    Servers 是一个简单的字符串数组,不是复杂类型。

    【讨论】:

    【解决方案2】:

    要使用你原来的类,你可以把你的 json 改成这个。 IMO,这更具可读性。

    {
         "Logging": {
              "IncludeScopes": false,
              "LogLevel": {
                   "Default": "Debug",
                   "System": "Information",
                   "Microsoft": "Information"
              }
         },
         "Servers": {
            "Names": [
              "SCHVW2K12R2-DB",
              "SCHVW2K12R2-DB\\MSSQL2016",
              "SCHVW2K8R2-DB"
            ]
         }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-29
      • 2017-07-17
      • 2018-10-05
      • 2021-11-09
      • 2019-01-26
      相关资源
      最近更新 更多