在appsettings.json
{
"MyArray": [
"str1",
"str2",
"str3"
]
}
在Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<IConfiguration>(Configuration);
}
在HomeController
public class HomeController : Controller
{
private readonly IConfiguration _config;
public HomeController(IConfiguration config)
{
this._config = config;
}
public IActionResult Index()
{
return Json(_config.GetSection("MyArray"));
}
}
上面是我的代码。结果是零。
如何获取数组?
这是一个老问题了,但是我可以给出一个用c# 7标准更新的。net Core 2.1的答案。假设我只在appsettings.Development.json中有一个列表,例如:
"TestUsers": [
{
"UserName": "TestUser",
"Email": "Test@place.com",
"Password": "P@ssw0rd!"
},
{
"UserName": "TestUser2",
"Email": "Test2@place.com",
"Password": "P@ssw0rd!"
}
]
我可以在Microsoft.Extensions.Configuration.IConfiguration被实现并像这样连接的任何地方提取它们:
var testUsers = Configuration.GetSection("TestUsers")
.GetChildren()
.ToList()
//Named tuple returns, new in C# 7
.Select(x =>
(
x.GetValue<string>("UserName"),
x.GetValue<string>("Email"),
x.GetValue<string>("Password")
)
)
.ToList<(string UserName, string Email, string Password)>();
现在我有了一个良好类型对象的列表。如果我点击testUsers.First(), Visual Studio现在应该会显示“用户名”、“电子邮件”和“密码”选项。
这为我工作,从我的配置返回一个字符串数组:
var allowedMethods = Configuration.GetSection("AppSettings:CORS-Settings:Allow-Methods")
.Get<string[]>();
我的配置部分是这样的:
"AppSettings": {
"CORS-Settings": {
"Allow-Origins": [ "http://localhost:8000" ],
"Allow-Methods": [ "OPTIONS","GET","HEAD","POST","PUT","DELETE" ]
}
}
这对我很管用;
创建json文件:
{
"keyGroups": [
{
"Name": "group1",
"keys": [
"user3",
"user4"
]
},
{
"Name": "feature2And3",
"keys": [
"user3",
"user4"
]
},
{
"Name": "feature5Group",
"keys": [
"user5"
]
}
]
}
然后,定义一些映射类:
public class KeyGroup
{
public string name { get; set; }
public List<String> keys { get; set; }
}
nuget packages:
Microsoft.Extentions.Configuration.Binder 3.1.3
Microsoft.Extentions.Configuration 3.1.3
Microsoft.Extentions.Configuration.json 3.1.3
然后加载它:
using Microsoft.Extensions.Configuration;
using System.Linq;
using System.Collections.Generic;
ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
configurationBuilder.AddJsonFile("keygroup.json", optional: true, reloadOnChange: true);
IConfigurationRoot config = configurationBuilder.Build();
var sectionKeyGroups =
config.GetSection("keyGroups");
List<KeyGroup> keyGroups =
sectionKeyGroups.Get<List<KeyGroup>>();
Dictionary<String, KeyGroup> dict =
keyGroups = keyGroups.ToDictionary(kg => kg.name, kg => kg);
在应用程序设置中添加一个级别。json:
{
"MySettings": {
"MyArray": [
"str1",
"str2",
"str3"
]
}
}
创建一个代表你的section的类:
public class MySettings
{
public List<string> MyArray {get; set;}
}
在你的应用启动类中,绑定你的模型并将其注入到DI服务中:
services.Configure<MySettings>(options => Configuration.GetSection("MySettings").Bind(options));
在你的控制器中,从DI服务中获取配置数据:
public class HomeController : Controller
{
private readonly List<string> _myArray;
public HomeController(IOptions<MySettings> mySettings)
{
_myArray = mySettings.Value.MyArray;
}
public IActionResult Index()
{
return Json(_myArray);
}
}
你也可以把你的整个配置模型存储在控制器的属性中,如果你需要所有的数据:
public class HomeController : Controller
{
private readonly MySettings _mySettings;
public HomeController(IOptions<MySettings> mySettings)
{
_mySettings = mySettings.Value;
}
public IActionResult Index()
{
return Json(_mySettings.MyArray);
}
}
ASP。NET Core的依赖注入服务就像一个魔法一样:)
灯塔3.1
Json配置:
"TestUsers":
{
"User": [
{
"UserName": "TestUser",
"Email": "Test@place.com",
"Password": "P@ssw0rd!"
},
{
"UserName": "TestUser2",
"Email": "Test2@place.com",
"Password": "P@ssw0rd!"
}]
}
然后创建一个User.cs类,它具有与上面Json配置中的User对象对应的auto属性。然后你可以引用Microsoft.Extensions.Configuration.Abstractions并执行以下操作:
List<User> myTestUsers = Config.GetSection("TestUsers").GetSection("User").Get<List<User>>();