The appsettings.json file is a main configuration file for any ASP.NET application.
It is a JSON format file used in ASP.NET applications to store configuration values.
These values can include database connection strings, API keys, logging configurations, and any other data we need for our application to function.
Do not store sensitive data in appsettings.json: For API keys, passwords, and other sensitive data, use Azure Key Vault or User Secrets in development.
Creating the appsettings.json file
When you create a new ASP.NET Core project, the appsettings.json file is automatically generated in the project’s root.
If it’s missing at some point, it’s okay. You can simply create it manually.
A typical appsettings.json file has a structure similar to the following:
{ “Logging”: { “LogLevel”: { “Default”: “Information”, “Microsoft”: “Warning”, “Microsoft.Hosting.Lifetime”: “Information” } }, “AllowedHosts”: ”*”, “ConnectionStrings”: { “DefaultConnection”: “Server=localhost;Database=MyDatabase;User Id=sa;Password=MyPassword;” }, “AppSettings”: { “ApiKey”: “12345-ABCDE-67890-FGHIJ”, “MaxItemsPerPage”: 50, “FeatureFlags”: { “EnableNewFeature”: true, “EnableExperimentalFeature”: false } } }
In this example:
- Logging: Configures the application’s logging level.
- AllowedHosts: Defines the allowed hosts for the application.
- ConnectionStrings: Contains database connection strings.
- AppSettings: Stores custom application configurations.
To add custom configurations, simply add new sections and keys to the file.
Accessing configuration values in code
Once we have defined our configurations in appsettings.json, we need to access and read these values from our code.
ASP.NET Core makes this easy thanks to the IConfiguration interface, which is automatically injected by the dependency injection system.
public class MyService
{
private readonly IConfiguration _configuration;
public MyService(IConfiguration configuration)
{
_configuration = configuration;
}
public void PrintApiKey()
{
var apiKey = _configuration["AppSettings:ApiKey"];
Console.WriteLine($"API Key: {apiKey}");
}
}
In this example, we access the ApiKey value using the key AppSettings:ApiKey. The : character is used to navigate the JSON file hierarchy.
Strongly typed configuration
It is also possible to apply strongly typed configurations, by mapping the JSON to a C# class.
This can contribute to code clarity and prevent errors (and makes you feel more at ease if you are addicted to typing 😋)
To do this, first define the class that represents your configuration:
public class AppSettings
{
public string ApiKey { get; set; }
public int MaxItemsPerPage { get; set; }
public FeatureFlags FeatureFlags { get; set; }
}
public class FeatureFlags
{
public bool EnableNewFeature { get; set; }
public bool EnableExperimentalFeature { get; set; }
}
Now in the Program.cs file, we configure the binding with appsettings.json,
public class Startup
{
public IConfiguration Configuration { get; }
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public void ConfigureServices(IServiceCollection services)
{
services.Configure<AppSettings>(Configuration.GetSection("AppSettings"));
}
}
Finally, access it with IOptions<T>:
public class MyService
{
private readonly AppSettings _appSettings;
public MyService(IOptions<AppSettings> appSettings)
{
_appSettings = appSettings.Value;
}
public void PrintApiKey()
{
Console.WriteLine($"API Key: {_appSettings.ApiKey}");
}
}
