Search code examples
c#asp.netappsettings

.NET 8 Core use Dictionary<Uri, Uri> in appsettings.json


I use IOptions pattern for some of my classes as a configuration. And I need to use Dictionary<Uri, Uri> inside this options class:

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

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

and this is how I bind these options:

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

The appsettings.json contains data as follows:

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

But however I change json I get an empty dictionary. I also tried to use simple Dictionary<string, string> instead of Dictionary<Uri, Uri>, but get same result. What's missing in here?


Solution

  • The issue is the ':' character that the addresses contain, it is not allowed in keys

    read more here https://github.com/dotnet/runtime/issues/42643