How to Read appsettings.json Values Using IOptions in .NET Core

Özkan ARDİL
6 min readJun 9, 2024

--

There are various ways to read the configuration settings from the appsettings.json file in .NET Core projects. In this article, I will tell you how to read the appsettings.json file content using IOptions in a .NET Core project.

In the world of C# and .NET Core, configuration management is critical to ensuring that an application is flexible and easily configurable.

In .NET Core projects, configuration settings are typically stored in an appsettings.json file.

When it comes to reading the configuration settings in the appsettings.json file, IOptions comes into play as one of the options.

IOptions is a mechanism that allows you to type-secure and retrieve your application’s configuration data.

What is IOptions?

The IOptions<T> interface allows you to use configuration data in a strongly typed way in .NET Core. represents a special class in which configuration data will be stored. This class is typically used to model the data in the file appsettings.json.

IOptions is used in the following scenarios:

Accessing Configuration Data: It is used to retrieve and use settings stored in configuration files with powerful types when you need to use them in various places throughout the application.

Ensuring Type Safety: binds configuration settings to a specific class, enabling compile-time error checking when using the settings. This prevents misspellings and type incompatibilities.

Centralized Management: It allows common settings to be managed between different components of the application from a central point. This ensures that when configuration changes are made, they are automatically updated throughout the app.

The use of IOptions is usually carried out in three steps.

Let me explain what we will do through the sample project I have prepared for understanding the subject. I will share the link to access the source codes for the project in the rest of the article.

So let’s continue with the project.

Below, you’ll find the folder structure of the project.

The folder structure of the sample project

I mentioned that there are different ways to read the appsettings.json file. Apart from IOptions, it is also possible to use access approaches using dependency injection or model class.

Please check the following articles to get more information.

The appsettings.json file in the project is already created by .NET Core by default during the project’s creation phase. You can also see the contents of the file below. Since it is a learning project, I wrote test texts in fields such as database and connection string. In real projects, of course, these areas need to be updated appropriately.

{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"ApplicationSettings": {
"DbConnString": "Using options connection string",
"SubSettings": {
"Name": "Read appsettings.json using options",
"ApiVersion": 3
}
}
}

IOptions Usage

Its use is usually carried out in three steps:

Step 1 — Configuration Class Definition:

First, a class is defined that will hold the configuration data. This class must match the structure in the appsettings.json file.

I also created a configuration class in the project as follows:

 public class AppsettingsOptionsModel
{
public string DbConnString { get; set; }
public AppsettingsSubsettingsModel? SubSettings { get; set; }
}

public class AppsettingsSubsettingsModel
{
public string Name { get; set; }
public string ApiVersion { get; set; }
}

Step 2 — Adding the Configuration to the Service:

In the Program.cs file, the configuration service is added and linked to the configuration file.

builder.Services.Configure<AppsettingsOptionsModel>(builder.Configuration.GetSection("ApplicationSettings"));

Step 3 — Using the Configuration:

The IOptions configuration settings are accessed, and data can be read when needed.

The most common method is to use dependency injection to access the IOptions interface.

 public class ReadAppsettingsUsingOptionsController : ControllerBase
{
private readonly AppsettingsOptionsModel _appSettingsModel;
public ReadAppsettingsUsingOptionsController(IOptions<AppsettingsOptionsModel> options)
{
_appSettingsModel = options.Value;
}

[HttpGet("ReadConnString")]
public IActionResult GetConnectionString()
{
return Ok(_appSettingsModel.DbConnString);
}

[HttpGet("ReadAppName")]
public IActionResult GetAppName()
{
return Ok(_appSettingsModel.SubSettings.Name);
}

[HttpGet("ReadGroupDate")]
public IActionResult GetGroupData()
{
return Ok(_appSettingsModel);
}
}

Now, let’s run our sample project and examine the results.

When we run the project, the Swagger interface welcomes us.

Each time endPoint is triggered, I have shared the returned results in order below.

The result of the method that reads the connection string value from the appsettings.json file
The result of the method reading the sub-node value from the appsettings.json file
The result of the method that reads the group value from the appsettings.json file

Reading the appsettings.json file with IOptions offers significant advantages such as type security, centralized management, and flexibility. However, it also has some drawbacks, such as initial costs, overloaded configuration classes, and the need for dynamic updates. Therefore, it is important for the developer to evaluate the use of IOptions according to the needs of the project.

In large and complex projects, IOptions are often a great benefit, while in small projects, simpler configuration methods may suffice.

Source Code

You can access the source code for my project on Github. I would be very pleased if you gave stars to the project and followed me.

Result

I tried to explain “Reading the appsettings.json file using IOptions”, which is one of the methods we can use to read appsettings.json files in .NET Core projects.

👏 If you found the content useful, you can follow me and make me happy in this way.

💬 If there is something I overlooked, you can share it in the comments or contact me on my profile.

⬇️ Check out my other articles.

💬 Let me know in the comment section what have I missed? Where I was wrong?

👨‍👦‍👦 You can also share this article with your friend. Argue a little on it. It is known that the truth is born in a dispute.

🙃 Stay determined, stay focused, and keep going

Thanks for reading…

--

--

Özkan ARDİL

.NET C# JS and Angular dev with 8+ yrs exp, self-taught & passionate web developer. Sharing tips & experiences in C# and web dev.