.NET 8 Core 在 appsettings.json 中使用 Dictionary<Uri, Uri>

问题描述 投票:0回答:1

我对我的一些类使用 IOptions 模式作为配置。我需要在这个选项类中使用

Dictionary<Uri, Uri>

public class CdnOptions
{
    public const string CONFIG_SECTION_NAME_S = "CdnTransformationOptions";

    public bool EnableCdnTransformations { get; set; }
    public Dictionary<string, string> CdnTransformRules { get; set; } = [];
}

这就是我绑定这些选项的方式:

services.AddOptions<CdnOptions>().Bind(configuration.GetSection(CdnOptions.CONFIG_SECTION_NAME_S))
            .ValidateDataAnnotations();

appsettings.json包含数据如下:

CdnTransformationOptions": {
 "EnableCdnTransformations": true,
 "CdnTransformRules": {
   "https://localhost:7021": "https://cdn.mydomain.com"
 }
},

但是无论我如何更改 json,我都会得到一个空字典。我也尝试使用简单的

Dictionary<string, string>
而不是
Dictionary<Uri, Uri>
,但得到相同的结果。 这里缺少什么?

c# asp.net appsettings
1个回答
0
投票

问题是地址包含“:”字符,键中不允许使用

在此处了解更多信息https://github.com/dotnet/runtime/issues/42643

© www.soinside.com 2019 - 2024. All rights reserved.