1

I want to populate a drop down list with a list of servers I have in a json file. So I want to retrieve the list from json and use that list to add all of the servers to my drop down. I have a simple json file with a simple array in it. In an ideal world, all I want to do is something like this:

public static IConfigurationRoot Configuration { get; set; }
public static void Main(string[] args = null)
{
    var builder = new ConfigurationBuilder()
        .AddJsonFile("appsettings.json");

    Configuration = builder.Build();
    List<string> servers = Configuration["servers"];

    foreach (string server in servers)
        {
            cboSelectServer.Items.Add(server);
        }
}

And my appsettings.json file looks like this:

{
    "servers": ["server1", "server2"]
}

The message I get is

Cannot implicitly convert type 'string' to System.Collections.Generic.List<string>

What is really confusing me is that it obviously understands Configuration["servers"] is a list because if I look at Configuration["servers:0"] I get my first server.

4
  • try this .... Configuration["servers"].Split(',').ToList() Commented Jun 7, 2017 at 11:17
  • Thanks @PraneshJanarthanan, but ToList() isn't an option when I add .Split(',') Commented Jun 7, 2017 at 11:19
  • Your appsettings.json contains list of string arrays, so you set value based on that. dummy code representation: List[0] => array.Split(',').ToList() Commented Jun 7, 2017 at 11:33
  • @SkinnyPete63 using System.Linq; Commented Jun 7, 2017 at 11:59

1 Answer 1

1

You need to make use of the GetChildren call from a ConfigurationSection as returned by GetSection:

var builder = new ConfigurationBuilder()
    .AddJsonFile("appsettings.json");

Configuration = builder.Build();
List<string> servers = Configuration.GetSection("servers")
                                    .GetChildren()
                                    .Select(x => x.Value)
                                    .ToList()

foreach (string server in servers)
    {
        cboSelectServer.Items.Add(server);
    }

You'll need a couple of using's at the top of your file:

using Microsoft.Extensions.Configuration;
using System.Linq;
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks Jamie, I am no programmer, just a hack trying to get by, so please bear with me. I have added the class in, no problem, but when I add the method IOptions gets underlined and there is no quick action to tell me how to resolve it. Also, is that a method of the class, or just an additional method in Main? Finally, services (as in services.Configure etc) also gets underlined with no quick action.
@SkinnyPete63 is this in an MVC/WebApi project? And if so, are you trying to consume this config data in a controller?
No, it's just a console application.
Ah right - this is the wrong answer then. Looking again .
Awesome, exactly what I was looking for. Thanks so much for your assistance.

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.