---
title: "How does configuration work in ASP.NET Core?"  
description: "How does configuration work in ASP.NET Core?"  
author: "Utpal Vishwas"  
published: 2025-05-20  
updated: 2025-05-20  
canonical: https://www.mindstick.com/interview/34147/how-does-configuration-work-in-asp-dot-net-core  
category: "c#"  
tags: ["c#", "api(s)", ".net core"]  
reading_time: 4 minutes  

---

# How does configuration work in ASP.NET Core?

In **ASP.NET Core**, configuration is **centralized and flexible**, allowing you to load settings from multiple sources like:

1. `appsettings.json`
2. Environment variables
3. Command-line arguments
4. Secrets (for development)
5. Custom providers (e.g., Azure Key Vault)

## Key Concepts

### 1. Configuration is Loaded at Startup

Configuration is typically built in `Program.cs` using:

```cs
var builder = WebApplication.CreateBuilder(args);
```

This line automatically loads configuration from:

1. `appsettings.json`
2. `appsettings.{Environment}.json`
3. Environment variables
4. Command-line args

## Common Configuration Sources

| Source | Purpose |
| --- | --- |
| `appsettings.json` | Default config (key-value pairs) |
| `appsettings.Development.json` | Overrides for dev environment |
| Environment variables | Useful for containerized/cloud setups |
| Command-line args | For runtime overrides |
| User secrets | Secure storage of dev-only secrets |

## appsettings.json Example

```plaintext
{
  "AppSettings": {
    "SiteName": "MySite",
    "PageSize": 10
  }
}
```

## Accessing Configuration

### 1. Via `IConfiguration`

```cs
public class HomeController : Controller
{
    private readonly IConfiguration _config;

    public HomeController(IConfiguration config)
    {
        _config = config;
    }

    public IActionResult Index()
    {
        string siteName = _config["AppSettings:SiteName"];
        return Content($"Welcome to {siteName}");
    }
}
```

### 2. Using Strongly-Typed Settings

#### a. Create a class:

```cs
public class AppSettings
{
    public string SiteName { get; set; }
    public int PageSize { get; set; }
}
```

#### b. Bind in `Program.cs`:

```cs
builder.Services.Configure<AppSettings>(builder.Configuration.GetSection("AppSettings"));
```

#### c. Inject via `IOptions<AppSettings>`:

```cs
public class HomeController : Controller
{
    private readonly AppSettings _settings;

    public HomeController(IOptions<AppSettings> options)
    {
        _settings = options.Value;
    }

    public IActionResult Index()
    {
        return Content($"Page size is {_settings.PageSize}");
    }
}
```

## Environment-based Config Files

ASP.NET Core supports automatic environment configuration:

```cs
var builder = WebApplication.CreateBuilder(args);
```

This loads:

- `appsettings.json`
- `appsettings.Development.json` (based on `ASPNETCORE_ENVIRONMENT`)

Set environment via:

```plaintext
set ASPNETCORE_ENVIRONMENT=Development
```

## Summary

| Concept | How it Works |
| --- | --- |
| Load settings | `builder.Configuration` |
| Inject settings | Via `IConfiguration` or `IOptions<T>` |
| Environment support | `appsettings.{Env}.json` auto-loaded |
| Secure secrets | Use `dotnet user-secrets` in development |

## Answers

### Answer by Utpal Vishwas

In **ASP.NET Core**, configuration is **centralized and flexible**, allowing you to load settings from multiple sources like:

1. `appsettings.json`
2. Environment variables
3. Command-line arguments
4. Secrets (for development)
5. Custom providers (e.g., Azure Key Vault)

## Key Concepts

### 1. Configuration is Loaded at Startup

Configuration is typically built in `Program.cs` using:

```cs
var builder = WebApplication.CreateBuilder(args);
```

This line automatically loads configuration from:

1. `appsettings.json`
2. `appsettings.{Environment}.json`
3. Environment variables
4. Command-line args

## Common Configuration Sources

| Source | Purpose |
| --- | --- |
| `appsettings.json` | Default config (key-value pairs) |
| `appsettings.Development.json` | Overrides for dev environment |
| Environment variables | Useful for containerized/cloud setups |
| Command-line args | For runtime overrides |
| User secrets | Secure storage of dev-only secrets |

## appsettings.json Example

```plaintext
{
  "AppSettings": {
    "SiteName": "MySite",
    "PageSize": 10
  }
}
```

## Accessing Configuration

### 1. Via `IConfiguration`

```cs
public class HomeController : Controller
{
    private readonly IConfiguration _config;

    public HomeController(IConfiguration config)
    {
        _config = config;
    }

    public IActionResult Index()
    {
        string siteName = _config["AppSettings:SiteName"];
        return Content($"Welcome to {siteName}");
    }
}
```

### 2. Using Strongly-Typed Settings

#### a. Create a class:

```cs
public class AppSettings
{
    public string SiteName { get; set; }
    public int PageSize { get; set; }
}
```

#### b. Bind in `Program.cs`:

```cs
builder.Services.Configure<AppSettings>(builder.Configuration.GetSection("AppSettings"));
```

#### c. Inject via `IOptions<AppSettings>`:

```cs
public class HomeController : Controller
{
    private readonly AppSettings _settings;

    public HomeController(IOptions<AppSettings> options)
    {
        _settings = options.Value;
    }

    public IActionResult Index()
    {
        return Content($"Page size is {_settings.PageSize}");
    }
}
```

## Environment-based Config Files

ASP.NET Core supports automatic environment configuration:

```cs
var builder = WebApplication.CreateBuilder(args);
```

This loads:

- `appsettings.json`
- `appsettings.Development.json` (based on `ASPNETCORE_ENVIRONMENT`)

Set environment via:

```plaintext
set ASPNETCORE_ENVIRONMENT=Development
```

## Summary

| Concept | How it Works |
| --- | --- |
| Load settings | `builder.Configuration` |
| Inject settings | Via `IConfiguration` or `IOptions<T>` |
| Environment support | `appsettings.{Env}.json` auto-loaded |
| Secure secrets | Use `dotnet user-secrets` in development |


---

Original Source: https://www.mindstick.com/interview/34147/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.
