---
title: "How to use appsettings.json in Asp.net core?"  
description: "How to use appsettings.json in Asp.net core?"  
author: "Utpal Vishwas"  
published: 2023-07-06  
updated: 2023-07-07  
canonical: https://www.mindstick.com/forum/158985/how-to-use-appsettings-json-in-asp-dot-net-core  
category: ".net core"  
tags: ["asp.net", "asp.net core", ".net core"]  
reading_time: 2 minutes  

---

# How to use appsettings.json in Asp.net core?

How to use [appsettings.json](https://www.mindstick.com/forum/157446/how-to-read-values-from-the-appsettings-json-file) in [Asp.net core](https://www.mindstick.com/articles/12946/get-started-with-asp-dot-net-core-mvc-and-visual-studio)?

## Replies

### Reply by Aryan Kumar

The `appsettings.json` file is used to store configuration settings for an [ASP.NET](https://www.mindstick.com/articles/934/default-folders-available-inside-the-asp-dot-net-application-folder) Core application. These settings can be used to configure anything from database connection strings to application secrets.

To use the `appsettings.json` file, you first need to add it to your project. You can do this by right-clicking on your project in Solution Explorer and selecting **Add** > **New Item**. From the list of templates, select **App Settings File** and click **Add**.

Once the `appsettings.json` file has been added, you can start adding configuration settings to it. Each setting is defined as a key-value pair. The key is the name of the setting and the value is the value of the setting.

For example, to define a database connection string, you would add the following setting to the `appsettings.json` file:

Code snippet

```plaintext
"ConnectionStrings": {
    "DefaultConnection": "Data Source=localhost;Initial Catalog=MyDatabase;Integrated Security=True"
}
```

Once you have added your configuration settings to the `appsettings.json` file, you can access them in your code using the `IConfiguration` service. The `IConfiguration` service is provided by the ASP.NET Core framework and it can be injected into your classes using dependency injection.

To access a configuration setting, you can use the `GetSection()` method. The `GetSection()` method takes the name of the section as its parameter and returns an `IConfigurationSection` object. The `IConfigurationSection` object contains all of the settings in the specified section.

For example, to get the database connection string, you would use the following code:

Code snippet

```plaintext
var configuration = DependencyResolver.Current.GetService<IConfiguration>();
var section = configuration.GetSection("ConnectionStrings");
var connectionString = section["DefaultConnection"];
```

The `connectionString` variable will now contain the value of the `DefaultConnection` setting in the `ConnectionStrings` section of the `appsettings.json` file.

You can also use the `IConfiguration` service to get individual configuration settings by using the `GetValue()` method. The `GetValue()` method takes the name of the setting and the default value as its parameters. If the setting is not found in the configuration file, the default value will be returned.

For example, to get the value of the `MySetting` setting, you would use the following code:

Code snippet

```plaintext
var configuration = DependencyResolver.Current.GetService<IConfiguration>();
var value = configuration.GetValue("MySetting", "Default Value");
```

The `value` variable will now contain the value of the `MySetting` setting, or the default value if the setting is not found in the configuration file.


---

Original Source: https://www.mindstick.com/forum/158985/how-to-use-appsettings-json-in-asp-dot-net-core

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
