55

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?

0

2 Answers 2

105

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.

Sign up to request clarification or add additional context in comments.

6 Comments

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
@NicolaPrada, yep, that's true. That's why you should not use Value in this particular case, but parse array with Get<string[]>() extension method.
This should be the accepted answer. It solves the problem as stated by the OP. Solved my problem, too.
i agree, this is the best answer, simple and effective
Great answer! Just an FYI: This pattern also works with List<string> if you want to return that instead of a string[]: section.Get<List<string>>()
|
47

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

Works like this:

var someArray = configuration
   .GetSection("SomeArray")
   .GetChildren()
   .Select(x => x.Value)
   .ToArray();

1 Comment

i like this way . it is easier and more comprehensible

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.