Recommended Free Tools
You can read configuration in an ASP.NET Core controller through dependency injection. Inject IConfiguration for a quick, one-off key lookup, using the indexer, sections, or GetConnectionString. For a related group of settings, Microsoft currently recommends the options pattern: bind a typed class, validate it when appropriate, and inject the resulting options into the controller.
The examples below reproduce the useful technique from Neel Bhatt’s February 11, 2018 quick post while separating that historical example from current ASP.NET Core guidance.
What the original .NET Core quick post demonstrates
Bhatt’s tutorial places a myFirstKey value in appsettings.json, injects IConfiguration into an MVC controller, and reads the value with a string key. The post’s prose mentions IConfigurationService, but the code uses the actual framework interface, IConfiguration. Its Contact fragment is abbreviated, so this is a complete compiling version:
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
public class HomeController : Controller
{
private readonly IConfiguration _configuration;
public HomeController(IConfiguration configuration)
{
_configuration = configuration;
}
public IActionResult Contact()
{
var value = _configuration["myFirstKey"];
return Content(value ?? "Key not found");
}
}
With this JSON:
{
"myFirstKey": "myFirstValue"
}
the action reads myFirstValue. The original article is Neel Bhatt’s “Read configuration in the controller: .Net Core Quick posts”, published February 11, 2018.
#1 Best Overall
Reading nested configuration values
Use a colon-delimited key
Configuration keys use a hierarchy separator. For nested JSON such as:
{
"Features": {
"Checkout": {
"Enabled": true
}
}
}
read the value with:
bool enabled = _configuration.GetValue<bool>("Features:Checkout:Enabled");
// Or, for the raw string representation:
string? raw = _configuration["Features:Checkout:Enabled"];
Microsoft documents the colon separator for hierarchical keys. You can also navigate to a section:
var checkout = _configuration.GetSection("Features:Checkout");
var enabled = checkout.GetValue<bool>("Enabled");
See Microsoft’s ASP.NET Core configuration documentation for the current provider and key rules.
Retrieve a named connection string
Connection strings conventionally live beneath ConnectionStrings:
Free tools Windows power users keep installed
One-click scans. No signup required.
Rank #3
{
"ConnectionStrings": {
"AppDatabase": "configured-by-the-deployment"
}
}
In a controller, the 2018 example’s pattern is:
string? connectionString =
_configuration.GetConnectionString("AppDatabase");
The returned value depends on the providers and environment used by the application. Keep real credentials out of source control; use an appropriate secret or deployment configuration provider.
Direct IConfiguration access versus typed options
Direct injection works, but Microsoft’s controller dependency-injection guidance says, “The options pattern described in Options pattern in ASP.NET Core is the preferred approach to manage settings” and, generally, “don’t directly inject IConfiguration into a controller.” That is a design recommendation, not a prohibition.
| Situation | Suitable approach | Reason |
|---|---|---|
| One occasional value | IConfiguration indexer or GetValue<T> |
Minimal code for a simple lookup |
| Several related settings | Bind a typed options class | Strongly typed access and a clear settings boundary |
| Settings require checks | Options with validation | Detect invalid configuration during application startup or resolution, according to the configured validation approach |
| Reload and lifetime behavior matters | Select the appropriate options interface | The options system provides interfaces for different access and reload needs; follow the documentation for the target framework |
Microsoft explains the options pattern in Options pattern in ASP.NET Core. It is the better default for controller-managed groups of settings because the controller depends on a meaningful application type rather than on the entire configuration tree.
Binding a configuration section to options
Define the settings class
public sealed class CheckoutOptions
{
public const string SectionName = "Features:Checkout";
public bool Enabled { get; set; }
public int MaximumItems { get; set; }
}
Register the binding
Register the section in the application’s service configuration using the options APIs available in your target ASP.NET Core version:
Best Value
builder.Services
.AddOptions<CheckoutOptions>()
.Bind(builder.Configuration.GetSection(CheckoutOptions.SectionName));
Inject the options into the controller
using Microsoft.Extensions.Options;
public class CheckoutController : Controller
{
private readonly CheckoutOptions _options;
public CheckoutController(IOptions<CheckoutOptions> options)
{
_options = options.Value;
}
public IActionResult Index()
{
return _options.Enabled ? View() : NotFound();
}
}
Add validation when the application must reject missing or out-of-range values, and choose the options interface that matches the required lifetime and reload behavior. Check the options documentation for the exact APIs supported by the framework version you target.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Configuration sources and why a value may be overridden
appsettings.json is only one possible source. ASP.NET Core can combine JSON files, environment variables, command-line arguments, user secrets, Azure configuration or secret providers, key-per-file, memory, and custom providers. The last provider added that supplies a key wins.
- An environment-specific JSON file loaded after the base file can replace a base value.
- Environment variables use double underscores (
__) to represent a hierarchy separator, soFeatures__Checkout__Enabledmaps toFeatures:Checkout:Enabled. - Command-line or deployment-injected values can override values that appear correct in source-controlled JSON.
If the controller returns an unexpected value, inspect the configured providers and their order before blaming dependency injection or the indexer. The unified configuration model and precedence rules are described in ASP.NET Core configuration and .NET configuration.
What is version-specific in the 2018 post
The original article discusses .NET Core 2.0 and constructor injection in Startup. That discussion reflects the hosting model of that period. Do not apply it unchanged to every current ASP.NET Core application: hosting and registration APIs differ across framework generations. Use documentation matching the target version, while retaining the general principles that configuration is composed from providers and that controllers can receive services through dependency injection.
Quick Recap
Practical decision checklist
- Use
IConfiguration["Key"]orGetValue<T>for a genuinely small, infrequent lookup. - Use
GetSectionwhen navigating a hierarchy without yet introducing a typed model. - Use
GetConnectionString("Name")for conventionally stored connection strings. - Bind related values to an options class when a controller needs a settings group.
- Add options validation for required, bounded, or security-sensitive settings.
- When values differ by environment, check provider order and environment-variable names.
- Keep secrets in deployment-appropriate secret management rather than committed JSON.
Sources
- Neel Bhatt, “Read configuration in the controller: .Net Core Quick posts” (February 11, 2018).
- Microsoft Learn: Dependency injection into controllers in ASP.NET Core.
- Microsoft Learn: Configuration in ASP.NET Core.
- Microsoft Learn: Options pattern in ASP.NET Core.
- Microsoft Learn: Configuration – .NET.
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

