Chapter 2: Customizing App Configuration
This second chapter is about application configuration, how to use it, and how to customize the ASP.NET configuration to employ different ways to configure your app. Perhaps you already have an existing XML configuration, or want to share a YAML configuration file over different kinds of applications. Sometimes, it also makes sense to read configuration values out of a database.
In this chapter, we will be covering the following topics:
- Configuring the configuration
- Using typed configurations
- Configuration using INI files
- Configuration providers
The topics in this chapter refer to the hosting layer of the ASP.NET Core architecture:

Figure 2.1 – ASP.NET Core architecture
Technical requirements
To follow the descriptions in this chapter, you will need to create an ASP.NET Core MVC application. Open your console, shell, or bash terminal, and change to your working directory. Use the following command to create a new MVC application:
Now, open the project in Visual Studio by double-clicking the project file or, in VS Code, by typing the following command in the already open console:
All of the code samples in this chapter can be found in the GitHub repository for this book at https://github.com/PacktPublishing/Customizing-ASP.NET-Core-5.0/tree/main/Chapter02.
Configuring the configuration
Let's start by looking at how to configure your various configuration options.
Since ASP.NET Core 2.0, the configuration is hidden in the default configuration of WebHostBuilder
, and no longer part of Startup.cs
. This helps to keep the startup clean and simple.
In ASP.NET Core 3.1 and ASP.NET Core 5.0, the code looks like this:
Fortunately, you are also able to override the default settings to customize the configuration in a way you need it.
When you create a new ASP.NET Core project, you will already have appsettings.json
and appsettings.Development.json
configured. You can, and should, use these configuration files to configure your app; this is the pre-configured way, and most ASP.NET Core developers will look for an appsettings.json
file to configure the application. This is absolutely fine and works pretty well.
The following code snippet shows the encapsulated default configuration to read the appsettings.json
files:
This configuration also sets the base path of the application and adds the configuration via environment variables. The ConfigureAppConfiguration
method accepts a lambda method that gets WebHostBuilderContext
and ConfigurationBuilder
passed in.
Whenever you customize the application configuration, you should add the configuration via environment variables as a final step, using the AddEnvironmentVariables()
method. The order of the configuration matters, and the configuration providers that you add later on will override the configurations added previously. Be sure that the environment variables always override the configurations that are set via a file. This way, you also ensure that the configuration of your application on an Azure App Service will be passed to the application as environment variables.
IConfigurationBuilder
has a lot of extension methods to add more configurations, such as XML or INI configuration files, and in-memory configurations. You can find additional configuration providers built by the community to read in YAML files, database values, and a lot more. In an upcoming section, we will see how to read INI files. First, we will look at using typed configurations.