C# 如何使用IConfigurationSectio.Get-in.netCore映射属性名称?
我在映射配置文件中与我要构建的类不同的名称时遇到了一些麻烦 我的配置文件:C# 如何使用IConfigurationSectio.Get-in.netCore映射属性名称?,c#,asp.net-core,.net-core,C#,Asp.net Core,.net Core,我在映射配置文件中与我要构建的类不同的名称时遇到了一些麻烦 我的配置文件: "148/FOEConfiguration":{ "EndPointUrl1":"http://10.116.105.25:8088/RPC2", "EndPointUrl2":"http://10.116.105.25:8088/RPC2", "FoeVersion":"6", "CryptoId":"6", "CryptoKey":"test", "NumberOf
"148/FOEConfiguration":{
"EndPointUrl1":"http://10.116.105.25:8088/RPC2",
"EndPointUrl2":"http://10.116.105.25:8088/RPC2",
"FoeVersion":"6",
"CryptoId":"6",
"CryptoKey":"test",
"NumberOfRetries":3,
"OrderVersion":"2",
"FOE.PODName":"FOE0001",
"FOE.TimeoutInMs":123,
"FOE.IsReadySuccCacheTtl":0,
"FOE.IsReadyStoreFailCacheTtl":0,
"FOE.IsReadyOrderFailCacheTtl":0
}
我在做什么
var section = _config.GetSection($"{restaurantNumber}/FOEConfiguration");
var restaurant = section.Get<RestaurantConfiguration>();
在配置文件中是FOE.timeoutims
如何将属性映射到配置文件中的名称?配置值到属性的映射是基本映射。无法自定义映射本身。但是,您可以为强类型配置创建自定义注册:
services.Configure<RestaurantConfiguration>(c => {
var config = Configuration.GetSection($"{restaurantNumber}/FOEConfiguration");
c.TimeoutInMs = config.GetValue<int>("FOE.TimeoutInMs");
// etc.
});
这些是配置文件,为什么不使它们相等?我看这里没有什么问题。您可以使用[JsonProperty]重命名属性,或者修复JSON。有什么原因不能更改配置文件中的属性名称吗?配置文件是由第三方生成的,我无法控制它。而且,c属性不能具有。以我的名义。有一些变通方法,但我想知道是否有一种方法可以绘制地图并使之更容易。我试着使用[JsonProperty],但它不起作用。这就是我的工作方式,我只是想知道是否有最好的方法来做到这一点。谢谢你,克里斯!
services.Configure<RestaurantConfiguration>(c => {
var config = Configuration.GetSection($"{restaurantNumber}/FOEConfiguration");
c.TimeoutInMs = config.GetValue<int>("FOE.TimeoutInMs");
// etc.
});