⚙️ Introduction
In ASP.NET Core development, configuration plays a very important role. Instead of hardcoding values in code, developers use a configuration system to manage settings like database connections, logging levels, API keys, and environment-specific values. The most commonly used configuration file is appsettings.json, and the main way to read these settings in your application is through IConfiguration. In this article, we will explain in simple terms how to use appsettings.json and IConfiguration with examples. All content is written to be SEO and GEO-friendly, so developers worldwide can easily understand.
📄 What is appsettings.json?
The appsettings.json file is a JSON (JavaScript Object Notation) file used in ASP.NET Core applications to store configuration data. It acts like a central place where you keep application settings that may change depending on the environment.
Key Features
Human-readable JSON format.
Hierarchical structure (supports nested sections).
Supports multiple environment files (e.g., appsettings.Development.json, appsettings.Production.json).
Automatically loaded by ASP.NET Core at startup.
Example of appsettings.json
{
"ConnectionStrings": {
"DefaultConnection": "Server=localhost;Database=mydb;User Id=sa;Password=YourPassword;"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning"
}
},
"AppSettings": {
"ApplicationName": "My ASP.NET Core App",
"Version": "1.0"
}
}
In this example
ConnectionStrings
store's database details.
Logging
defines how much log detail should be shown.
AppSettings
stores custom application values.
🛠️ What is IConfiguration?
The IConfiguration interface in ASP.NET Core is used to read values from appsettings.json (and other sources like environment variables, command-line arguments, or secrets). It provides a simple way to access settings by key.
Key Features
Reads data from appsettings.json, environment variables, and more.
Supports hierarchical keys (e.g., Logging:LogLevel:Default
).
Allows strongly typed configuration with classes.
Example of using IConfiguration
public class HomeController : Controller
{
private readonly IConfiguration _configuration;
public HomeController(IConfiguration configuration)
{
_configuration = configuration;
}
public IActionResult Index()
{
var appName = _configuration["AppSettings:ApplicationName"];
var version = _configuration["AppSettings:Version"];
ViewData["Message"] = $"{appName} - Version {version}";
return View();
}
}
Here, we are reading ApplicationName and Version from appsettings.json using IConfiguration
.
⚡ Using Strongly Typed Classes for Configuration
Instead of accessing settings by string keys, ASP.NET Core allows binding configuration to classes. This makes the code cleaner, type-safe, and easier to maintain.
Example
public class AppSettings
{
public string ApplicationName { get; set; }
public string Version { get; set; }
}
In Program.cs
:
builder.Services.Configure<AppSettings>(builder.Configuration.GetSection("AppSettings"));
In a Controller or Service:
public class HomeController : Controller
{
private readonly AppSettings _appSettings;
public HomeController(IOptions<AppSettings> appSettings)
{
_appSettings = appSettings.Value;
}
public IActionResult Index()
{
return Content($"App Name: {_appSettings.ApplicationName}, Version: {_appSettings.Version}");
}
}
This approach is recommended for large applications because it makes managing configuration much easier.
🌍 Environment-Specific Configurations
ASP.NET Core supports different appsettings.json files for different environments such as Development, Staging, and Production.
Example of appsettings.Development.json
{
"AppSettings": {
"ApplicationName": "My Dev App",
"Version": "1.0-DEV"
}
}
When the application runs in Development mode, it will override values from appsettings.json with values from appsettings.Development.json.
This makes it easy to have different settings for local testing, staging servers, and live production environments.
🔐 Security Best Practices
When using appsettings.json, it is important to handle sensitive information carefully.
Do not store passwords or API keys directly in appsettings.json for production.
Use User Secrets in development.
Use Azure Key Vault, AWS Secrets Manager, or environment variables for production.
Example: Instead of keeping your database password in plain text, store it in an environment variable and let ASP.NET Core automatically load it into IConfiguration
.
✅ Summary
appsettings.json is the main file for configuration in ASP.NET Core, and IConfiguration is the service that reads these values. Developers can store important settings like connection strings, API keys, logging levels, and app information in appsettings.json. With the help of IConfiguration and strongly typed classes, these values can be accessed easily inside the application. By using environment-specific files and following security best practices, developers can build applications that are secure, flexible, and ready for global deployment. This approach makes ASP.NET Core applications scalable, maintainable, and cloud-ready.