配置訪問介面 public IConfiguration _Config; public 類名 (IConfiguration Config) { _Config = Config; } 配置文件數據示例 { "AllowedHosts": "*", "Users": [ { "Id": "123" ...
配置訪問介面
public IConfiguration _Config; public 類名 (IConfiguration Config) { _Config = Config; }
配置文件數據示例
{ "AllowedHosts": "*", "Users": [ { "Id": "123", "Username": "me" }, { "Id": "456", "Username": "notme" } ] }
var allowedHosts=_Config["AllowedHosts"]; // * var allowedHosts=_Config["Users:1:Id"]; // 123 var allowedHosts=_Config["Users:2:Id"]; // 456 //獲取整個數組的所有值 var UserData = _Config.GetSection("Users"); var UserArray = UserData.AsEnumerable();
基於Json數組創建Users
模型
public class Users { public string Id { get; set; } public string Username { get; set; } }
List<Users> TestUsers = _Config.GetSection("Users").Get<List<Users>>();