我在appsettings.json
...
{
"Environments": [
{
"Name": "One",
"CloudServices": [
"name1",
"name2"
]
},
{
"Name": "Two",
"CloudServices": [
"name3",
"name4"
]
}
]
}
...以及以下强类型模型:
public class EnvironmentModel
{
public string Name { get; set; }
public string[] CloudServices { get; set; }
}
public class EnvironmentsConfig
{
public EnvironmentModel[] Environments { get; set; }
}
我希望我能在Startup
这样绑定这个:
services.Configure<EnvironmentsConfig>(Configuration.GetSection("Environments"));
但是,当以IOptions<EnvironmentsConfig>
注入我的控制器时,该值为null。是否可以像这样绑定强类型数组?如果没有,推荐的方法是什么?部署之间的环境数量可能不同,因此我需要一个阵列。
答案 0 :(得分:2)
配置的Environments
部分包含EnvironmentModel
的数组,而不是EnvironmentsConfig
。要修复它,您只需使用配置的根而不是部分。例如:
services.Configure<EnvironmentsConfig>(Configuration);