Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion docs/core/extensions/configuration.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: Configuration
description: Learn how to use the Configuration API to configure .NET applications. Explore various inbuilt configuration providers.
ms.date: 10/09/2024
ms.date: 08/27/2026
ms.topic: overview
ai-usage: ai-assisted
---
Expand Down Expand Up @@ -118,6 +118,7 @@ The configuration binder has specific behaviors and limitations when working wit
- [Bind to dictionaries](#bind-to-dictionaries)
- [Dictionary keys with colons](#dictionary-keys-with-colons)
- [Bind to IReadOnly* types](#bind-to-ireadonly-types)
- [Exclude properties from binding](#exclude-properties-from-binding)
- [Bind with parameterized constructors](#bind-with-parameterized-constructors)

#### Bind to dictionaries
Expand Down Expand Up @@ -152,6 +153,14 @@ The configuration class implementation:

This approach allows the binder to populate the mutable `List<string>` while presenting an immutable interface to consumers through `IReadOnlyList<string>`.

#### Exclude properties from binding

If you don't want the configuration binder to populate a property, apply <xref:Microsoft.Extensions.Configuration.ConfigurationIgnoreAttribute> to it (available starting in .NET 11).

:::code language="csharp" source="snippets/configuration/binding-scenarios/ConfigurationIgnore/Program.cs" id="AppOptions":::

If configuration contains values for both `Endpoint` and `RuntimeValue`, the binder updates `Endpoint` and leaves `RuntimeValue` at its existing value.

#### Bind with parameterized constructors

Starting with .NET 7, the configuration binder supports binding to types with a single public parameterized constructor. This enables immutable types and records to be populated directly from configuration:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net11.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// <AppOptions>
using Microsoft.Extensions.Configuration;

public sealed class AppOptions
{
public string Endpoint { get; set; } = "";

[ConfigurationIgnore]
public string RuntimeValue { get; set; } = "calculated";
}
// </AppOptions>

public static class Program
{
public static void Main()
{
var configuration = new ConfigurationManager
{
[nameof(AppOptions.Endpoint)] = "https://localhost",
[nameof(AppOptions.RuntimeValue)] = "configured",
};

var options = new AppOptions();
configuration.Bind(options);

Console.WriteLine($"Endpoint: {options.Endpoint}");
Console.WriteLine($"RuntimeValue: {options.RuntimeValue}");
}
}
Loading