43

I use VSCode and NetCore 1.1.1.

I need to store several datapaths in my appsetting.json to let my console application know where to look for its data.

This is an extract of the appsettings.json file:

{

    "ConnectionStrings":

    {

        "Database": "Filename=./Data/Database/securities_master.db"
    },

    "Data":

    {

     "Folders": ["E:/Data/Folder1/","E:/Data/Folder2/"]

    }
}

I load the configuration file and I want the "Folders" array stored in a variable:

const string APP_SETTINGS_SECTION = "Data";
const string APP_SETTINGS_KEY = "Folders";

var builder = new ConfigurationBuilder().AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
var configuration = builder.Build();
var dataFolders = configuration.GetSection(APP_SETTINGS_SECTION)[APP_SETTINGS_KEY];

dataFolders is NULL!

If I change my appsetting.json to point only to a single directory like this, everything works:

{

    "ConnectionStrings":

    {

        "Database": "Filename=./Data/Database/securities_master.db"
    },

    "Data":

    {

     "Folders": "E:/Data/Folder1/"   
    }
}

dataFolder = "E:/Data/Folder1/"

So the problem appears to be it doesn't like the string array but to me it looks like a valid Json string array. How should I modify my appsettings (or my C# code) to fix this?

nico9T
  • 2,496
  • 2
  • 26
  • 44

2 Answers2

91

Indexer of a section returns string by exact key match, and since array values have keys with postfixes, there is nothing to match given key and you are getting null.

To get it work you may use something like this

var section = configuration.GetSection($"{APP_SETTINGS_SECTION}:{APP_SETTINGS_KEY}");
var folders = section.Get<string[]>();

And check this for more options.

THTP
  • 1,336
  • 11
  • 6
  • 1
    var folder = configuration.GetSection(APP_SETTINGS_SECTION)[APP_SETTINGS_KEY]; and var folder2 = configuration.GetSection($"{APP_SETTINGS_SECTION}:{APP_SETTINGS_KEY}").Value; give you the same result: null If there is a string array stored in appsettings.json, a string if there is just a string – nico9T Mar 17 '17 at 14:03
  • 1
    @NicolaPrada, yep, that's true. That's why you should not use Value in this particular case, but parse array with Get() extension method. – THTP Mar 17 '17 at 14:44
  • 1
    This should be the accepted answer. It solves the problem as stated by the OP. Solved my problem, too. – Brad Jun 06 '17 at 01:41
  • 2
    i agree, this is the best answer, simple and effective – Giova Aug 04 '17 at 12:33
  • 3
    Great answer! Just an FYI: This pattern also works with List if you want to return that instead of a string[]: `section.Get>()` – Papa Stahl Jan 10 '18 at 15:33
  • thanks @THTP, just a short statement `configuration.GetSection("Cors:AllowedOrigin").Get();` – Luis Raúl Espinoza Barboza Aug 28 '20 at 16:56
37

Original answer from here: https://stackoverflow.com/a/42169474/7263255

Works like this:

var someArray = configuration
   .GetSection("SomeArray")
   .GetChildren()
   .Select(x => x.Value)
   .ToArray();
Halloween_Udo
  • 69
  • 2
  • 9