Learn how to fetch properties or values from appsettings.json in .NET Core Razor Pages and console projects, using both IConfiguration and Options Patterns.
All projects are Razor Pages except one console project. The majority of work is done with dependency injection.
Warning This article is not a step-by-step on how to work with appsettings.json so if that is what you are looking for you may be disappointed. The intent is to provide working code samples to learn from.
- If you are new to dependency injection see Dependency injection in ASP.NET Core
- Decide what to place in appsettings.json
- Do the values change will determine which interface to use.
- Examine include soure code
- Read Microsoft documentation, see the resource section at end of this article.
- Get stuck, ask on Stackoverflow
- User names and passwords
- Any sensitive information that may compromise data and/or the company. This may also include connection strings to databases unless protected by a DMZ for instance.
Working with vaults and different environments.
For working with different environments see Use multiple environments in ASP.NET Core which will assist a developer learning how to work with multiple environments. I see no reason to repeat good documentation.
Also see project EnvironmentApplication
Take time to review these projects, more likely than not there will be a solution to what a developer needs to store and read data.
VariousMethodsApplication
smorgasbord of code samples for obtaining information from appsettings.json- Index page: demonstrates how to read nested sections using
IOptions
andIOptionsSnapshot
- ApplicationFeaturesLoose page: demonstrates IConfiguration.Bind while in
ApplicationFeaturesStrong
the same section is accessed usingIOptionsSnapshot
which is a better option if the application needs to get fresh values if the appsetting.json file changes. - GetSectionExample page: provides sample code to read a section in appsettings.json
- NamedOption page variations on the Index Page
- OptionsMonitorExample page IOptionsMonitor code sample. Note there seems to be issues with the OnChange event.
- Index page: demonstrates how to read nested sections using
ConnectionStringApplication
is a simple example to get a connection string from appsettings.json for EF Core and using a data provider, in this case Microsoft.Data.SqlClient.ControlLoggingApplication
Example to toggle SeriLog on/off via appsettings.jsonDataAnnotatedValidationApplication
shows how to validate values in appsettings.json using Data Annotations validation in Program.csReadListApplication
example for reading an array/listReadSettingsConsoleApplication
sinple console project for showing reading settings from appsettings.jsonSectionExistsApplication
demonstrates how to check if a section exists in appsettings.jsonEnvironmentApplication
demonstrates reading a connection string dependent on the current environment.UpdaterApplication
how to update appsettings.json at runtimeGetWebAddressesApplication
the idea is to show a developer might store web addresses for services in appsettings.jsonExcelConnectionApplication
demonstrates how to read a connection string for Excel from appsettings.json
See the following article ASP.NET Core/Razor pages Secret Manager with source code in the following repository in the following project.
- Microsoft Visual Studio 2022 or higher
- .NET Core 8
The major difference is the lifetime of these instances. IOptionsMonitor is registered as Singleton, whereas the IOptionsSnapshot is registered as Scoped.
- Use IOptions, when you are not expecting your configuration values to change.
- Use IOptionsSnapshot when you expect your values to change, but want them to be uniform for the entire request cycle.
- Use IOptionsMonitor when you need real-time options data.
- Configuration in ASP.NET Core
- Options pattern in ASP.NET Core
- Use multiple environments in ASP.NET Core
- Get started with Razor Pages in ASP.NET Core
Clone the following GitHub repository.
https://dev.to/karenpayneoregon/storing-and-reading-values-from-appsettingsjson-io