我可以从完整 appsettings.json 中获取类型化的选项对象吗?
Can I get a typed Options object from full appsettings.json?
从 ASP.NET 核心文档看来我只能使用 IConfiguration::Bind<T>()
或 IConfiguration::Get<T>()
.
将配置的命名细分绑定到类型化对象
我想知道是否有办法将整个 appsettings.json
文件从根目录复制到类型对象中。
这可能吗?
如果是,我是否需要在键入的对象中将所有配置设置作为 public 属性提供,或者我是否可以省略其中的一些设置,活页夹将自动跳过我在键入的对象中未提供的项目目的? (我想其他提供商会添加大量我不想添加到我的 Options
对象中的配置设置。)
IConfigurationRoot
, which represents the root of the configuration and include all sections of your appsettings.json
, is also an IConfiguration
以便您可以在需要使用 IConfiguration
进行配置的地方使用它。
这意味着您可以在配置根上调用 Bind()
以将其完全绑定到一个对象,或者您可以使用 services.Configure()
方法使用完整的配置根配置选项类型:
services.Configure<AllOptions>(Configuration);
If it is, do I need to provide all configuration settings as public properties in my typed object or can I omit some of them and the binder will automatically skip items I don't provide in my typed object?
在绑定期间,绑定器将跳过它无法绑定到目标对象的值。所以是的,配置对象可以包含比目标类型支持的更多的值。
从 ASP.NET 核心文档看来我只能使用 IConfiguration::Bind<T>()
或 IConfiguration::Get<T>()
.
我想知道是否有办法将整个 appsettings.json
文件从根目录复制到类型对象中。
这可能吗?
如果是,我是否需要在键入的对象中将所有配置设置作为 public 属性提供,或者我是否可以省略其中的一些设置,活页夹将自动跳过我在键入的对象中未提供的项目目的? (我想其他提供商会添加大量我不想添加到我的 Options
对象中的配置设置。)
IConfigurationRoot
, which represents the root of the configuration and include all sections of your appsettings.json
, is also an IConfiguration
以便您可以在需要使用 IConfiguration
进行配置的地方使用它。
这意味着您可以在配置根上调用 Bind()
以将其完全绑定到一个对象,或者您可以使用 services.Configure()
方法使用完整的配置根配置选项类型:
services.Configure<AllOptions>(Configuration);
If it is, do I need to provide all configuration settings as public properties in my typed object or can I omit some of them and the binder will automatically skip items I don't provide in my typed object?
在绑定期间,绑定器将跳过它无法绑定到目标对象的值。所以是的,配置对象可以包含比目标类型支持的更多的值。