我有以下配置设置:
public void ConfigureServices(IServiceCollection services)
{
var configuration = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build();
services.Configure<AppSettings>(configuration);
}
public class AppSettings
{
public ChildSettings Child { get; set; }
public string Property { get; set; }
}
public class ChildSettings
{
public string ChildProperty { get; set; }
}
我的appSettings.json文件如下所示:
{
"Child": {
"ChildProperty": "Value"
}
"Property": "Value"
}
我可以在控制器中轻松地注入 IOptions<AppSettings>
:
public class MyController : Controller
{
public MyController(IOptions<AppSettings> options)
{
ChildSettings appSettings = options.Value;
}
}
每次都要下到几个层级才能找到想要的设置对象,这已经有点陈旧了。我能否这样使用 IOptions<ChildSettings>
:
public class MyController : Controller
{
public MyController(IOptions<ChildSettings> options)
{
ChildSettings appSettings = options.Value;
}
}