建议将设置值数组保存在 .net 核心 Appsettings 文件中

Array of settings values are recommended to keep inside a .net core Appsettings file

我正在构建一个控制台应用程序,我必须保存几个设置值,其中一些是值数组我想的 Appsettings 示例部分如下所示

    "FolderSettings":{
            "source": "c:\sourcefolder",
            "target": "c:\targetfolder"
    },
    "FolderJPG":[
        {
        "name":"foldername",
        "width":1450,
        "height":1450
        }
    ],
    "FolderPNG":[
        {
        "name":"foldername",
        "width":300,
        "height":300
        },
        {
        "name":"foldername2",
        "width":450,
        "height":450
        }
    ]

是否建议在 appsettings 文件 [本例中为 FolderJPG 和 FolderPNG] 中保存设置值数组,或者是否有任何推荐的方法将此类设置保存在 .net 核心中?

此外,我如何获取设置值作为值数组?我知道读取一个简单的键值对的方式就像

    _configuration.GetValue<string>("FolderSettings:source");  
    

但是我怎样才能正确读取 FolderJPG、FolderPNG 等的设置值数组?

绑定到模型

public class FolderOption
{
   public string Name { get; set; }
   public int Width{ get; set; }
   public int Height{ get; set; }
}

然后

_configuration.GetSection("FolderJPG").Get<List<FolderOption>>(); 

appsettings.json 可以包含您需要但有限制的任何配置 - 它应该是 JSON.

JSON objects are written in key/value pairs.

Keys must be strings, and values must be a valid JSON data type (string, number, object, array, boolean or null).

所以,数组只是值的数据类型