Ullan
Ullan

Reputation: 1007

How to read a section from appsettings.json using .Net core console application?

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.

Upvotes: 0

Views: 866

Answers (1)

Bart
Bart

Reputation: 21

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

Upvotes: 2

Related Questions