---
title: "How to read values from the Appsettings.json file?"  
description: "How to read values from the Appsettings.json file?"  
author: "Ashutosh Patel"  
published: 2023-03-06  
updated: 2023-07-08  
canonical: https://www.mindstick.com/forum/157446/how-to-read-values-from-the-appsettings-json-file  
category: "asp.net core"  
tags: ["asp.net", "asp.net core"]  
reading_time: 3 minutes  

---

# How to read values from the Appsettings.json file?

How to read [values](https://www.mindstick.com/forum/327/sum-textbox-values) from the [Appsettings.json](https://www.mindstick.com/forum/157452/what-is-the-purpose-of-the-appsettings-json-file) [file](https://www.mindstick.com/articles/59/encrypting-and-decrypting-files-using-c-sharp)?

## Replies

### Reply by Aryan Kumar

To read values from the appsettings.json file in ASP.NET Core, you can use the `IConfiguration` interface. The `IConfiguration` interface provides a way to access configuration data from within your application.

To read a value from the appsettings.json file, you can use the `[]` operator. The `[]` operator takes two parameters: the name of the value that you want to read and the default value that you want to use if the value is not found in the configuration file.

The following is an example of how to read a value from the appsettings.json file:

C#

```plaintext
using Microsoft.AspNetCore.Hosting;

namespace MyApp
{
    public class Program
    {
        public static void Main(string[] args)
        {
            // Create a configuration object
            var configuration = new ConfigurationBuilder()
                .AddCommandLine(args)
                .Build();

            // Get the database connection string from the configuration
            string connectionString = configuration["ConnectionStrings:MyDatabase"];

            // Use the database connection string to connect to the database
            var database = new MyDatabase(connectionString);

            // Do something with the database
            database.DoSomething();
        }
    }
}
```

In this example, we first create a configuration object by calling the `ConfigurationBuilder()` constructor. We then add the command-line arguments to the configuration object by calling the `AddCommandLine()` method. Finally, we get the database connection string from the configuration object by calling the `["ConnectionStrings:MyDatabase"]` property.

We can then use the database connection string to connect to the database and do something with the database.

If the value that you are trying to read is not found in the configuration file, the `IConfiguration` interface will return the default value that you specified.

You can also use the `GetSection()` method to read a section from the appsettings.json file. A section is a group of related configuration values. For example, you could have a section for database connection strings, a section for API keys, and so on.

The following is an example of how to read a section from the appsettings.json file:

C#

```plaintext
using Microsoft.AspNetCore.Hosting;

namespace MyApp
{
    public class Program
    {
        public static void Main(string[] args)
        {
            // Create a configuration object
            var configuration = new ConfigurationBuilder()
                .AddCommandLine(args)
                .Build();

            // Get the database connection strings section from the configuration
            var connectionStrings = configuration.GetSection("ConnectionStrings");

            // Get the database connection string from the section
            string connectionString = connectionStrings["MyDatabase"];

            // Use the database connection string to connect to the database
            var database = new MyDatabase(connectionString);

            // Do something with the database
            database.DoSomething();
        }
    }
}
```

In this example, we first create a configuration object by calling the `ConfigurationBuilder()` constructor. We then add the command-line arguments to the configuration object by calling the `AddCommandLine()` method. Finally, we get the database connection strings section from the configuration object by calling the `GetSection()` method.

We can then get the database connection string from the section by calling the `["MyDatabase"]` property.

If the section that you are trying to read is not found in the configuration file, the `IConfiguration` interface will return null.


---

Original Source: https://www.mindstick.com/forum/157446/how-to-read-values-from-the-appsettings-json-file

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
