为ASP.NET Core中的选项自定义JSON属性名称 [英] Customize JSON property name for options in ASP.NET Core

查看:641
本文介绍了为ASP.NET Core中的选项自定义JSON属性名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从JSON文件加载配置时,我想使用其他属性名称进行配置.

I want to use different property names for configuration, when loading the configuration from a JSON file.

public class MinioConfiguration
{
    [DataMember(Name = "MINIO_ENDPOINT")]
    public string Endpoint { get; set; }
    [DataMember(Name = "MINIO_ACCESS_KEY")]
    public string AccessKey { get; set; }
    [DataMember(Name = "MINIO_SECRET_KEY")]
    public string SecretKey { get; set; }
}

DataMemberJsonProperty均不起作用.有可能实现它吗?

Neither DataMember nor JsonProperty work. Is there a possibility to achieve it?

推荐答案

不幸的是,这不可能.这里负责的组件是JSON配置提供程序(Microsoft.Extensions.Configuration.Json)和配置绑定程序(Microsoft.Extensions.Configuration.Binder).前者负责将JSON文件加载到IConfiguration中,而后者的工作是将IConfiguration映射到类型中.

This is unfortunately not possible. The responsible components here are the JSON configuration provider (Microsoft.Extensions.Configuration.Json) and the configuration binder (Microsoft.Extensions.Configuration.Binder). The former is responsible for loading the JSON file into an IConfiguration, while the latter’s job is to map an IConfiguration into a type.

但是,配置绑定器与源无关,因此它并不关心配置来自何处,无论是JSON,内存中配置还是环境变量.因此,根据设计,活页夹无法在目标类型上查找特定于源的属性.

The configuration binder however is source agnostic, so it does not care where the configuration came from, whether that being JSON, an in-memory configuration or environment variables. So the binder, by design, cannot look for source-specific attributes on the target type.

另一方面,不能保证将配置绑定到任何类型,因此无论配置提供程序是否绑定到类型,它都需要一致地工作.因此,JSON提供程序遵循其自己的约定将JSON属性映射到配置键.

On the other hand, configurations are not guaranteed to be bound to any type, so the configuration provider needs to work consistently regardless of whether it binds to a type or not. So the JSON provider follows its own conventions to map JSON properties to configuration keys.

由于两个组件都独立运行-绑定程序甚至在IConfiguration对象上明确运行,因此它的运行时间比实际配置解析过程晚得多,因此它们无法通信和共享信息.因此,JSON配置提供程序根本无法访问该信息,而活页夹会在很晚之后获得有关目标类型的信息.因此,您根本无法更改配置名称.

Since both components run independently—the binder running even explicitly on the IConfiguration object, so it runs much later than the actual configuration parsing process—they cannot communicate and share information. So it’s simply not possible for the JSON configuration provider to access the information, the binder much later gets about the target type. As such, you simply cannot change the configuration names.

但是,我认为无论如何都不应该这样做.配置是一个非常特定于应用程序的事情,但是如果您想使用看起来像是要使用应用程序不一定拥有的配置文件的其他密钥.我建议不要使用这种方法,而应使用特定于应用程序的配置文件.

However, I would argue that this is not something that should be done anyway. The configuration is a very application-specific thing, but if you want to use different keys that looks like you want to use a configuration file that is not necessarily owned by the application. I would recommend against this approach and rather use an application-specific configuration file.

如果需要重用该配置文件,则始终可以为该特定类型编写自己的配置提供程序.编写配置提供程序并不困难,特别是当您要将其限制为非常特定的格式时.

If you need to reuse that configuration file, you could always write your own configuration provider for that specific type. Writing a configuration provider is not that difficult, especially when you are going to restrict it to a very specific format anyway.

这篇关于为ASP.NET Core中的选项自定义JSON属性名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆