appsettings.json 文件内容:
{ "Logging": { "LogLevel": { "Default": "Information", "Microsoft.AspNetCore": "Warning" } }, "MyConfigKey": "MyConfigValue", "AllowedHosts": "*", "UserInfo": [ { "id": 0, "password": "123456", "realName": "Vben", "roles": [ "super" ], "username": "vben" }, { "id": 1, "password": "123456", "realName": "Admin", "roles": [ "admin" ], "username": "admin" }, { "id": 2, "password": "123456", "realName": "Jack", "roles": [ "user" ], "username": "jack" } ], "MOCK_CODES": [ // super { "codes": [ "AC_100100", "AC_100110", "AC_100120", "AC_100010" ], "username": "vben" }, { // admin "codes": [ "AC_100010", "AC_100020", "AC_100030" ], "username": "admin" }, { // user "codes": [ "AC_1000001", "AC_1000002" ], "username": "jack" } ] }
需要在控制器中获取 UserInfo 集合
1. 在Program.cs中添加 appsettings.json加载配置
builder.Configuration.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
2.在控制器中引入
3,在控制器获取 UserInfo 集合内容
[HttpGet] public List<LoginSuccess> GetConfiguration() { return _configuration.GetSection("UserInfo").Get<List<LoginSuccess>>(); }
4, LoginSuccess.cs内容
public class LoginSuccess { /// <summary> /// 用户id /// </summary> public int Id { get; set; } /// <summary> /// 密码 /// </summary> public string Password { get; set; } /// <summary> /// 用户昵称 /// </summary> public string RealName { get; set; } /// <summary> /// 用户角色 /// </summary> public List<string> Roles { get; set; } /// <summary> /// 用户名 /// </summary> public string Username { get; set; } }
5,发起请求