在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"));
}
}
上面有我的代码,我为null如何获取数组?
"Clients": [ {..}, {..} ]
,则应调用Configuration.GetSection("Clients").GetChildren()
。