You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: aspnetcore/fundamentals/configuration/options.md
+60-7Lines changed: 60 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,20 +5,15 @@ description: Discover how to use the options pattern to represent groups of rela
5
5
monikerRange: '>= aspnetcore-3.1'
6
6
ms.author: tdykstra
7
7
ms.custom: mvc
8
-
ms.date: 04/20/2025
8
+
ms.date: 01/15/2026
9
9
uid: fundamentals/configuration/options
10
10
---
11
11
# Options pattern in ASP.NET Core
12
12
13
13
[!INCLUDE[](~/includes/not-latest-version.md)]
14
14
15
-
<!-- Update [!INCLUDE[](~/includes/bind7.md)]
16
-
when updating this article -->
17
-
18
15
:::moniker range=">= aspnetcore-7.0"
19
16
20
-
By [Rick Anderson](https://twitter.com/RickAndMSFT).
21
-
22
17
The options pattern uses classes to provide strongly typed access to groups of related settings. When [configuration settings](xref:fundamentals/configuration/index) are isolated by scenario into separate classes, the app adheres to two important software engineering principles:
@@ -34,7 +29,65 @@ This article provides information on the options pattern in ASP.NET Core. For in
34
29
35
30
## Bind hierarchical configuration
36
31
37
-
[!INCLUDE[](~/includes/bind7.md)]
32
+
The preferred way to read related configuration values is using the [options pattern](xref:fundamentals/configuration/options). For example, to read the following configuration values:
* Has public read-write properties of the type that have corresponding items in config are bound.
49
+
* Has its read-write properties bound to matching entries in configuration.
50
+
* Does ***not*** have its fields bound. In the preceding code, `Position` is not bound. The `Position` field is used so the string `"Position"` doesn't need to be hard coded in the app when binding the class to a configuration provider.
51
+
52
+
The following code:
53
+
54
+
* Calls [ConfigurationBinder.Bind](xref:Microsoft.Extensions.Configuration.ConfigurationBinder.Bind*) to bind the `PositionOptions` class to the `Position` section.
In the preceding code, by default, changes to the JSON configuration file after the app has started are read.
60
+
61
+
[`ConfigurationBinder.Get<T>`](xref:Microsoft.Extensions.Configuration.ConfigurationBinder.Get*) binds and returns the specified type. `ConfigurationBinder.Get<T>` may be more convenient than using `ConfigurationBinder.Bind`. The following code shows how to use `ConfigurationBinder.Get<T>` with the `PositionOptions` class:
Calls to `Bind` are less strict than calls to `Get<>`:
76
+
77
+
* `Bind` allows the concretion of an abstract.
78
+
* `Get<>` has to create an instance itself.
79
+
80
+
## The Options Pattern
81
+
82
+
An alternative approach when using the ***options pattern*** is to bind the `Position` section and add it to the [dependency injection service container](xref:fundamentals/dependency-injection). In the following code, `PositionOptions` is added to the service container with <xref:Microsoft.Extensions.DependencyInjection.OptionsConfigurationServiceCollectionExtensions.Configure*> and bound to configuration:
In the preceding code, changes to the JSON configuration file after the app has started are ***not*** read. To read changes after the app has started, use [IOptionsSnapshot](xref:fundamentals/configuration/options#ios).
Copy file name to clipboardExpand all lines: aspnetcore/fundamentals/configuration/options/includes/options6.md
+43-3Lines changed: 43 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,5 @@
1
1
:::moniker range="= aspnetcore-6.0"
2
2
3
-
By [Kirk Larkin](https://twitter.com/serpent5) and [Rick Anderson](https://twitter.com/RickAndMSFT).
4
-
5
3
The options pattern uses classes to provide strongly typed access to groups of related settings. When [configuration settings](xref:fundamentals/configuration/index) are isolated by scenario into separate classes, the app adheres to two important software engineering principles:
@@ -17,7 +15,49 @@ This article provides information on the options pattern in ASP.NET Core. For in
17
15
18
16
## Bind hierarchical configuration
19
17
20
-
[!INCLUDE[](~/includes/bind6.md)]
18
+
The preferred way to read related configuration values is using the [options pattern](xref:fundamentals/configuration/options). For example, to read the following configuration values:
* Must be non-abstract with a public parameterless constructor.
34
+
* All public read-write properties of the type are bound.
35
+
* Fields are ***not*** bound. In the preceding code, `Position` is not bound. The `Position` field is used so the string `"Position"` doesn't need to be hard coded in the app when binding the class to a configuration provider.
36
+
37
+
The following code:
38
+
39
+
* Calls [ConfigurationBinder.Bind](xref:Microsoft.Extensions.Configuration.ConfigurationBinder.Bind*) to bind the `PositionOptions` class to the `Position` section.
In the preceding code, by default, changes to the JSON configuration file after the app has started are read.
45
+
46
+
[`ConfigurationBinder.Get<T>`](xref:Microsoft.Extensions.Configuration.ConfigurationBinder.Get*) binds and returns the specified type. `ConfigurationBinder.Get<T>` may be more convenient than using `ConfigurationBinder.Bind`. The following code shows how to use `ConfigurationBinder.Get<T>` with the `PositionOptions` class:
In the preceding code, by default, changes to the JSON configuration file after the app has started are read.
51
+
52
+
An alternative approach when using the ***options pattern*** is to bind the `Position` section and add it to the [dependency injection service container](xref:fundamentals/dependency-injection). In the following code, `PositionOptions` is added to the service container with <xref:Microsoft.Extensions.DependencyInjection.OptionsConfigurationServiceCollectionExtensions.Configure*> and bound to configuration:
In the preceding code, changes to the JSON configuration file after the app has started are ***not*** read. To read changes after the app has started, use [IOptionsSnapshot](xref:fundamentals/configuration/options#ios).
Copy file name to clipboardExpand all lines: aspnetcore/fundamentals/host/generic-host.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -207,7 +207,7 @@ To set this value, use the environment variable or configure `HostOptions`. The
207
207
208
208
### Disable app configuration reload on change
209
209
210
-
By [default](xref:fundamentals/configuration/index#default), `appsettings.json` and `appsettings.{Environment}.json` are reloaded when the file changes. To disable this reload behavior in .NET 5 or later, set the `hostBuilder:reloadConfigOnChange` key to `false`.
210
+
By [default](xref:fundamentals/configuration/index#default-app-configuration-sources), `appsettings.json` and `appsettings.{Environment}.json` are reloaded when the file changes. To disable this reload behavior in .NET 5 or later, set the `hostBuilder:reloadConfigOnChange` key to `false`.
211
211
212
212
**Key**: `hostBuilder:reloadConfigOnChange`
213
213
**Type**: `bool` (`true` or `false`)
@@ -675,7 +675,7 @@ To set this value, use the environment variable or configure `HostOptions`. The
675
675
676
676
### Disable app configuration reload on change
677
677
678
-
By [default](xref:fundamentals/configuration/index#default), `appsettings.json` and `appsettings.{Environment}.json` are reloaded when the file changes. To disable this reload behavior in .NET 5 or later, set the `hostBuilder:reloadConfigOnChange` key to `false`.
678
+
By [default](xref:fundamentals/configuration/index#default-app-configuration-sources), `appsettings.json` and `appsettings.{Environment}.json` are reloaded when the file changes. To disable this reload behavior in .NET 5 or later, set the `hostBuilder:reloadConfigOnChange` key to `false`.
0 commit comments