Azure 配置应用程序设置中的字典

Dictionary in Azure Configuration Application Settings

我在 .NET 核心中有一个示例应用程序,appsettings.json 文件用于存储配置。

在这个 json 文件中,我创建了一个层次结构,该层次结构将转换为 Dictionary 以便从我的代码中的某些方法访问。

到这里为止一切正常,没有问题。

现在,此应用托管在 Azure 中,我正在将所有设置迁移到 Azure 配置 - 应用程序设置部分。

第一级中的 appsettings 值在 Azure 设置部分中正常工作(例如:SampleSettingAddress)。

您知道应该如何将字典(OptionsDictionary、MusicUrls)放置在 Azure 设置字段中吗?可能吗?

     "SampleSetting": {
    
        "SampleSettingAddress": "sample",
    
        "SampleSettingEmailAddress": "sample",
    
     
    
        "MusicUrls": {
    
          "musicStore": "https://samplemusic1.com/hits/",
    
          "musicStoreAn": "https://samplemusic2.com/hits/",
    
          "musicStoreExt": "https://samplemusic3.com/hits/"
    
        },
    
        "OptionsDictionary": {
    
          "key1": "sample1",
    
          "key2": "sample2",
    
          "key3": "sample3"
    
        }
    
    }

 

 

public class SampleSetting

    {

        public string SampleSettingAddress { get; set; }

 

        public string SampleSettingEmailAddress { get; set; }

 

        public Dictionary<string, string> MusicUrls { get; set; }

                              

        public Dictionary<string, string> OptionsDictionary { get; set; }

                              

   }

 

    "SampleSetting": {
    
        "SampleSettingAddress": "sample",
    
        "SampleSettingEmailAddress": "sample",
    
     
    
        "MusicUrls": {
    
          "musicStore": "https://samplemusic1.com/hits/",
    
          "musicStoreAn": "https://samplemusic2.com/hits/",
    
          "musicStoreExt": "https://samplemusic3.com/hits/"
    
        },
    
        "OptionsDictionary": {
    
          "key1": "sample1",
    
          "key2": "sample2",
    
          "key3": "sample3"
    
        }
    
    }

 

 

public class SampleSetting

    {

        public string SampleSettingAddress { get; set; }

 

        public string SampleSettingEmailAddress { get; set; }

 

        public Dictionary<string, string> MusicUrls { get; set; }

                              

        public Dictionary<string, string> OptionsDictionary { get; set; }

                              

   }

当然,确实可以这样做。你会这样做:

Name: SampleSetting__SampleSettingAddress
Value: sample

Name: SampleSetting__MusicUrls__musicStore
Value: https://samplemusic1.com/hits/

基本上,您需要展平 JSON 对象并使用 __ 作为分隔符。如果您使用 Windows,您还可以使用 : 而不是 __