带有列表的asp.net核心ioptions

时间:2017-03-10 19:20:52

标签: c# json asp.net-core

我试图从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; }
}

2 个答案:

答案 0 :(得分:4)

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

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

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

答案 1 :(得分:2)

要使用原始类,可以将json更改为此。 IMO,这更具可读性。

<body ng-controller="MainCtrl">
    <div ng-if="selectedFeature">
      <span>Select 1: </span>
      <select ng-model="$root.selectedFeature" ng-options="feature for feature in features" ng-change="b()">
        </select>
    </div>
    <!--<input type="text" ng-model="a" ng-change="call(a)">-->
    <span>Select 2: </span>
    <select ng-model="$root.selectedRole" ng-options="role for role in roles" ng-change="call($root.selectedRole)">
    </select>
  </body>