Reading the appsettings.json File Using Model Class in .NET Core

Özkan ARDİL
5 min read4 days ago

There are various ways to read the configuration settings in the appsettings.json file in .NET Core projects. In this article, I will tell you how to read the appsettings.json file content using Model Class 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, using a Model Class is one of the alternate solutions.

I explained the other two approaches. The first one of them is “Using Dependency Injection,” and the second one is “Using IOptions Mechanism”.

Each approach has its own advantages and disadvantages.

Therefore, I recommend that you examine other approaches and use the solution that suits your project.

You can access related articles using the links below.

Without going into further details, I would like to point out that, in my projects, I generally prefer to use the static class approach that I will explain in this article.

The model class structure allows you to use configuration data in a strongly typed way in .NET Core. The model class works as a special class in which configuration data will be stored. This class is typically used to model the data in the file appsettings.json.

It is possible to use the model class 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.

I have prepared for understanding the subject.

You will find 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

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 Middleware connection string",
"SubSettings": {
"Name": "Read appsettings.json using Static Model",
"ApiVersion": 3
}
}
}

Model Class Usage

Its use is usually carried out in three steps:

Step 1 — Model Class Definition:

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

public class ApplicationSettings
{
public static string DbConnString { get; set; }
public static AppsettingsSubsettingsModel SubSettings { get; set; } = new AppsettingsSubsettingsModel();
}

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

Step 2 — Adding the Model Class to the Service:

In the Program.cs file, the model class is bound to the configuration.

builder.Configuration.GetSection("ApplicationSettings").Get<ApplicationSettings>();

Step 3 — Using the Model Class within the Project:

Since the model class is designed as static, it is possible to use the class without using dependency injection.

public class ReadAppsettingUsingModelController : ControllerBase
{

[HttpGet("ReadConnString")]
public IActionResult GetConnectionString()
{
var connString = ApplicationSettings.DbConnString;
return Ok(connString);
}

[HttpGet("ReadAppName")]
public IActionResult GetAppName()
{
var connString = ApplicationSettings.SubSettings.Name;
return Ok(connString);
}

[HttpGet("ReadGroup")]
public IActionResult GetGroup()
{
var subSettings = ApplicationSettings.SubSettings;
return Ok(subSettings);
}
}

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

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

Each time an 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 the static model class offers significant advantages such as type security, centralized management, and flexibility.

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 the static model class,” 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.