IOptions 与不匹配的 属性 名称绑定

IOptions binding with non-matching property names

是否可以将 JSON 文件 (appsettings.json) 中的属性绑定到使用不同 属性 名称的 class?

{
    "WebTarget": {
    "WebURL": "http://www.whosebug.com"
  }
}
public class MyServiceOptions
{
    public string Url { get; set; }
}

我想采用 WebURL 设置并将其映射到选项 class 中的 Url 属性。我试过 [DataMember][JsonProperty] 但它们不起作用。

我知道这并不理想,属性 名称应该与 JSON 中的名称相匹配,但这是一个特例。

是的,这是可能的。它需要更多的手动配置

services.Configure<MyServiceOptions>(myOptions => {
    myOptions.Url = Configuration.GetSection("WebTarget").GetValue<string>("WebURL", string.Empty);
});

引用Configure simple options with a delegate