c# - 带有列表的 asp.net 核心 ioptions

标签 c# json asp.net-core

我正在尝试从 appsettings.json 文件中读取值列表。我能够毫无问题地读取 Logging 值,但列表值(即 Servers)为空:

应用设置.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; }
}

最佳答案

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

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

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

关于c# - 带有列表的 asp.net 核心 ioptions,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42726171/

相关文章:

c# - 色表算法

c# - 我可以使用 Guid 的前 16 个或后 16 个字符并且它仍然是唯一的吗?

c# - 从异步中捕获未处理的异常

c# - 为什么 Entity Framework 需要 30 秒来加载记录,而生成的查询只需要 1/2 秒?

javascript - 使用 GUID 作为键缓存 "deep"JSON 对象

Javascript 数组 - 合并多个数组帮助

json - 在 ms sql (SQL Server) 中以\u0 格式 (json) 转义变音

asp.net-core - ASP.NET Core 中的 AntiForgeryConfig.UniqueClaimTypeIdentifier

c# - Asp.Net Core 和脚手架

asp.net-core - ASP.NET Core 2.2 中的路由本地化