Introduction
In today’s article, we will see how to read values from appsettings.json in ASP.NET Core, similar to what we used to do in ASP.NET MVC by reading values from web.config.
Why do we need this?
In a complex application, there are several constants that need to be used throughout an application, but we just cannot simply hard-code them at every place we need because if we need to change some value, then we will have to visit each and every place where it is used, and change it, which can be very risk-prone as we are bound to miss some.
Hence, to solve this issue, we keep such constants in a single location and access them wherever we need them.
In ASP.NET MVC, we used to keep such information in a web.config file, but in the ASP.NET Core application, there is no such thing as web.config.
So, how do we do it?
There is a file present for this purpose only which is called appsettings.json. We define our values in that file in key-value pairs, and then we use them as per our needs. So okay, let’s get started.
We will create the ASP.NET Core project first.

Then, we will pick API as our template (This is just a demonstration, you can pick any template you want).

Once the project is created, we will add an API Controller.

Now, we will open the appsettings.json file and add our own keys with their values.
I have added the following JSON object.
"MySettings": {
"DbConnection": "abc",
"Email": "[email protected]",
"SMTPPort": "5605"
}
My modified file looks like this.

Now, in our Controller that we created earlier, we will add a method so that we can test our code.
My dummy method looks like this.
[HttpGet]
public IActionResult GetUsers()
{
var list = new List<string>();
list.Add("John");
list.Add("Doe");
return Ok(list);
}
Now, in our cController, we will add a private field of type “IConfiguration” which is found in Microsoft.Extensions.Configuration namespace, so we need to add that namespace in our file.
Now, we will create a constructor of our Controller with the input parameter “IConfiguration”.
public UserController(IConfiguration iConfig)
{
configuration = iConfig;
}

The part you see in yellow is just a dummy method to get called when we run this API Controller. Now, we are all set up to get the values of our custom keys that we have defined in the appsettings.json file.
There are two methods to retrieve our values,
string dbConn = configuration.GetSection("MySettings").GetSection("DbConnection").Value;
In the first method, we are getting our section from the configuration object. Inside this, we are getting another section that contains the value.
string dbConn2 = configuration.GetValue<string>("MySettings:DbConnection"); 









Deepak RohillaPosted Jun 3, 2022, 4:57 AM
Thanks you for this article.
James CollettPosted Oct 25, 2021, 11:42 PM
Your section around the text 'So our modified Controller will look something like this' looks wrong.You refer to MySettings twice like so:- configuration.GetSection("MySettings").GetSection("MySettings").Value I think you mean to write:- configuration.GetSection("MySettings").GetSection("DbConnection").Value
James CollettPosted Sep 30, 2021, 10:42 AM
Thank you for this article. It got me out of a hole. It would be even better if your screenshot images were higher quality. The resolution is just a little low to read easily what is happening on the screen.
SidPosted Mar 24, 2021, 6:36 PM
What if the key is not present in appsettings?
Arjun ColbayPosted Nov 28, 2020, 11:00 AM
Thanks for the neat and simple presentation with enough screen shots, it helped me a lot.
jonathan sunPosted Aug 21, 2020, 4:31 PM
Great article. I followed each steps in this article, I can access the properties in the MySettingsModel that matches the settings in appSetting.json, but I got null values for each in the controller.
hoang hieuPosted Jul 6, 2020, 10:44 PM
How i can use the config value anywhere else in the application lets say the controllers or services?
Jahan AlemPosted Apr 8, 2020, 7:29 AM
Great solution!
Jahan AlemPosted Apr 8, 2020, 6:23 AM
"A1": { "B1": { "C1": "", "C2": "", "C3": "", "C4": "" } }, "A2": { "B2": { "C5": "", "C6": "", "C7": "" } }
Jahan AlemPosted Apr 8, 2020, 6:23 AM
Hi Ravi, Is it possible to write only one class for this Model:
Huy NhatPosted Mar 14, 2020, 11:39 AM
What a Great article!!!
Chittaranjan SwainPosted Sep 12, 2019, 6:42 AM
Nice article, thanks for sharing.
LORRAN PALMEIRAPosted Sep 19, 2018, 2:33 PM
How to do when I need access varible from appsettings.json to put in my atributte, for example: [MyAtributte(variable.appsetting.json)]