Reading the configuration File in ASP.NET Core

In this tutorial, let us learn how to read the configuration file using the ASP.NET Configuration Service. The ASP.NET core configuration systems allow us to add configurations from various sources using providers. It then converts the configurations into a key/value pair. We inject the configuration service into a class and then use the GetSection, Value, & GetValue methods to read the configurations. Also, learn how to parse the types. How to bind the configurations to a class instance. Learn how to use the Options pattern & IOptionsSnapshot to create a strongly-typed options object. Read configurations stored as arrays, read from INI, XML files, command-line arguments, etc.

Reading the Configuration

We use the dependency injection to get the instance of configuration service (using the interface IConfiguration ) in the constructor of the service class or controller class. We can also inject it into the startup class also.

The IConfiguration interface is available in the namespace Microsoft.Extensions.Configuration

The code is as shown below

Here’s a Sample JSON file with some configuration:

Boolean Values are stored as string True or False.

GetSection, Value, & GetValue methods

Use the GetSection & Value method to read a setting. Use the colon (:) to separate each section.

You can also chain the getSection

Reading a non existent setting returns NULL. No error is thrown.

Parsing Value to Type

Use parse method to get the correct type

or use the the built in GetValue method to parse the type.

Another way to use the GetValue

Binding to Objects

Reading each & every setting is a little time-consuming. Instead, we can read the entire section of values using the bind method. This also gives us a strongly typed configuration object.

First, create a class whose properties matches the configuration options. The following class has same properties as the appsettings.json of the previous section.

Create an instance of the above class. The use the Bind method to populate the class with values from the configurations.

Or you can use the get method

Using the Options Pattern

Options Pattern is another way by which we can read the configurations. This helps us to bind it to a class instance & also use DI to inject it into the class constructor.

First, create the class whose properties matches the configuration options as we did it in the previous section on binding to objects

Open the class or controller, where you want to inject the configuration. Import the namespace Microsoft.Extensions.Options.

Inject the IOptions<T> where T is the configuration class (customOptionsConfiguration.) that we want to inject.

Now, open the startup class.

Use the Configure<ApplicationOptions> to add the configurations to the service container. Pass the section to read using Configuration.GetSection("customOptions") , which binds it to that configuration.

IOptions<T> does not load the configuration data after the app has started. It is also registered as Singleton and hence can be injected into any service.

Use IOptionsSnapshot to read updated data

To Reload the Configuration on each request, use the IOptionsSnapshot<T>. In this case, the service is registered as scoped and therefore cannot be injected into Singleton Service.

Reading Arrays from the Configuration file

Consider the following array of configuration objects in json file.

The ASP.NET Core adds the keys based on number to each flattened element. It looks like this.

Reading from XML

The above format, but in XML is as shown below. Create appsettings.xml in project root folder and paste the following. Also, right-click go to properties and select copy always under the option copy to the output directory.

The default loader in ASP.NET Core does not load the XML files. Use the AddXmlFile extension method to load it in the program.cs.

The configuration system ignores the <root> element.

Reading from INI file

The above format, but in INI is as shown below. Create appsettings.ini in project root folder and paste the following. Also, right-click go to properties and select copy always under the option copy to the output directory.

Use the AddIniFile extension method to load it in the program.cs.

Reading from Command-line arguments

The above settings using the Command-line arguments.

Reading the Environment variables

Refer to the article Environment variables in ASP.NET Core

Order Matters

The Order in which configuration files are loaded is matters. If two configuration sources contain similar keys, the one which loads last wins.

The default configuration loader loads the configuration in the following order.

  1. appsettings.json
  2. Environment-specific appsettings.{EnvironmentName}.json
  3. Environment variables
  4. Command-line parameters


Options Pattern in ASP.NET Core

Read More

  1. Configuration in ASP.NET Core
  2. Environment variables in ASP.NET Core

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top