---
title: "How does Configuration work in ASP.NET Core?"  
description: "How does Configuration work in ASP.NET Core?"  
author: "Ashutosh Patel"  
published: 2023-03-06  
updated: 2023-07-08  
canonical: https://www.mindstick.com/forum/157447/how-does-configuration-work-in-asp-dot-net-core  
category: "asp.net core"  
tags: ["asp.net", "asp.net core"]  
reading_time: 2 minutes  

---

# How does Configuration work in ASP.NET Core?

How does [Configuration work in ASP.NET](https://www.mindstick.com/interview/34147/how-does-configuration-work-in-asp-dot-net-core) Core?

## Replies

### Reply by Aryan Kumar

In [ASP.NET Core](https://www.mindstick.com/articles/12946/get-started-with-asp-dot-net-core-mvc-and-visual-studio), [configuration](https://www.mindstick.com/articles/13112/setting-up-the-perfect-configuration-for-your-work-computer) is used to store application settings, such as database connection strings, API keys, and other configuration data.

The configuration system in ASP.NET Core is based on the `IConfiguration` interface. The `IConfiguration` interface provides a way to access configuration data from within your application.

There are a number of ways to add configuration data to an ASP.NET Core application. You can add configuration data to the application by:

- Adding configuration data to a configuration file, such as `appsettings.json` or `web.config`.
- Adding configuration data to the environment, such as environment variables or command-line arguments.
- Adding configuration data programmatically, using the `IConfiguration` interface.

Once you have added configuration data to your application, you can access it from within your application by using the `IConfiguration` interface.

The following is an example of how to access configuration data from within your application:

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.


---

Original Source: https://www.mindstick.com/forum/157447/how-does-configuration-work-in-asp-dot-net-core

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
