0

I have a appsettings.json file as below

{
  "student":{
     "subject": "0;3",
     "grade": "4;5"
   }
}

I want to read the section student and add it to a dictionary. My dictionary keys will be subject and grade. For that I am doing the following to get the section student. I don't want to define any class to store the section student.

            var config = new Microsoft.Extensions.Configuration.ConfigurationBuilder()
             .SetBasePath(Path.GetDirectoryName(Assembly.GetEntryAssembly()?.Location))
                .AddJsonFile("appsetting.json").Build();
        var result= config.GetSection("student");

But values are not getting populated in result.

enter image description here

Thanks for your help.

2
  • What's the reason for not wanting to define a data structure for your configuration? That generally helps simplify any code you're writing. Regardless of that, are you sure that the config file exists at the path that you are adding it from? Can you inspect the value of config to see if the student path exists? Commented May 16, 2022 at 4:38
  • 2
    Why would they be? That's the section, not the data in the section. Did you check to see what members are available on result and what those members do? Commented May 16, 2022 at 4:40

1 Answer 1

2

you should create a separate object that contains the values. check this documentation

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

Comments

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.