aspnet-core-appsettings-json

Configuration and appsettings.json file in ASP.NET

  • 3 min

The appsettings.json file is a main configuration file for any ASP.NET application.

It is a file in JSON format that is used in ASP.NET applications to store configuration values.

These values can include connection strings to databases, 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 root of the project.

If it’s not there at any point, that’s fine. 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 logging level of the application.
  • AllowedHosts: Defines the allowed hosts for the application.
  • ConnectionStrings: Contains the connection strings to databases.
  • AppSettings: Stores custom application configurations.

To add custom configurations, simply add the new sections and keys in 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 value of ApiKey using the key AppSettings:ApiKey. The character : is used to navigate through the hierarchy of the JSON file.

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 it makes you feel more at ease if you are a type addiction 😋)

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}");
    }
}