映射 appsettings.json 到字典 <string, Value>

Map appsettings.json to Dictionary<string, Value>

我有以下配置

"Options": {
  "Host": "123",
  "UserName": "test",
  "Password": "test",
  "Files": [
    {
      "Key": "asd",
      "Value": {
        "HostLocation": "asd",
        "RemoteLocation": "asd"
      }
    }
  ]
}

我正在尝试将它绑定到以下对象

public class Options
{
    public string Host { get; set; }
    public string UserName { get; set; }
    public string Password { get; set; }
    public Dictionary<string, FileOptions> Files { get; set; }

    public class FileOptions
    {
        public string HostLocation { get; set; }
        public string RemoteLocation { get; set; }
    }
}

问题是当我试图将文件绑定到字典时。他们不受约束。我得到一个值为 1 的键,并且值 FileOptions 都是用默认字符串值生成的。

这是我的配置映射。

_serviceCollection.Configure<SftpOptions>(_configuration.GetSection("Options"));

有什么问题以及如何将设置映射到选项 class。

They don't get bound. I get a key generated with the value of 1, and the value FileOptions are generated all with default string value.

这是正确的,因为 Files 是显示的数组 JSON

  "Files": [
    {
      "Key": "asd",
      "Value": {
        "HostLocation": "asd",
        "RemoteLocation": "asd"
      }
    }
  ]

JSON 需要如下所示才能满足所需的对象图

"Options": {
  "Host": "123",
  "UserName": "test",
  "Password": "test",
  "Files": {
      "asd": {
        "HostLocation": "asd",
        "RemoteLocation": "asd"
      },
      "someOtherKey" : {
        "HostLocation": "something",
        "RemoteLocation": "something"
      }
    }
  }
}