Configuration and Environments
ASP.NET Core configuration is built from a layered set of providers, appsettings.json, an environment-specific appsettings.{Environment}.json, environment variables, command-line arguments, and optionally secret stores, all merged into a single IConfiguration object. Providers added later override values set by earlier ones, so environment variables set on a production server can override a placeholder connection string checked into appsettings.json without changing any code.
Cricket analogy: It's like a team's final playing XI being built in layers, the base squad list, then injury updates, then last-minute pitch-condition changes, each layer overriding the previous one until the final team sheet is locked.
The IHostEnvironment and Environment Switching
ASP.NET Core reads the ASPNETCORE_ENVIRONMENT variable at startup to populate IWebHostEnvironment.EnvironmentName, which defaults to Production if unset and is commonly set to Development or Staging locally and in CI pipelines. Code frequently branches on this with app.Environment.IsDevelopment(), for example to enable the detailed exception page and Swagger UI locally while showing a generic error page and disabling Swagger in Production for security.
Cricket analogy: It's like a groundskeeper preparing a pitch differently depending on whether it's a practice net session or an actual Test match, the same ground, but the setup and safety checks differ sharply by context.
Strongly Typed Configuration with the Options Pattern
Rather than reading raw string keys like builder.Configuration['Smtp:Host'] scattered throughout the codebase, ASP.NET Core's Options pattern binds a configuration section to a plain C# class, for example builder.Services.Configure<SmtpOptions>(builder.Configuration.GetSection('Smtp')), and that class is then injected via IOptions<SmtpOptions> anywhere it's needed, giving you compile-time safety and IntelliSense instead of magic strings.
Cricket analogy: It's like a scorer using a structured official scorebook with defined columns for runs, wickets, and overs instead of scribbling notes on loose scraps of paper that anyone could misread.
public class SmtpOptions
{
public string Host { get; set; } = "";
public int Port { get; set; } = 587;
public string FromAddress { get; set; } = "";
}
// Program.cs
builder.Services.Configure<SmtpOptions>(
builder.Configuration.GetSection("Smtp"));
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
else
{
app.UseExceptionHandler("/error");
}
// Anywhere in the app
public class EmailSender
{
private readonly SmtpOptions _options;
public EmailSender(IOptions<SmtpOptions> options) => _options = options.Value;
}appsettings.Development.json is typically excluded from production deployments or overridden entirely; secrets like connection strings and API keys should live in environment variables, a secret manager, or Azure Key Vault, never committed to appsettings.json in source control.
- Configuration in ASP.NET Core is layered: later providers override earlier ones.
- Common sources include appsettings.json, appsettings.{Environment}.json, environment variables, and command-line args.
- ASPNETCORE_ENVIRONMENT controls IWebHostEnvironment.EnvironmentName and defaults to Production.
- app.Environment.IsDevelopment() is used to branch behavior like enabling Swagger or detailed errors.
- The Options pattern binds configuration sections to strongly typed C# classes.
- IOptions<T> is injected via DI to consume strongly typed configuration anywhere in the app.
- Secrets should never be committed to appsettings.json; use environment variables or a secret manager instead.
Practice what you learned
1. What happens when the same configuration key exists in both appsettings.json and an environment variable?
2. What is the default value of ASPNETCORE_ENVIRONMENT if it is not explicitly set?
3. What does the Options pattern (IOptions<T>) provide over reading raw configuration strings directly?
4. Where should a production database connection string with a real password be stored?
Was this page helpful?
You May Also Like
Dependency Injection Basics
How ASP.NET Core's built-in dependency injection container registers and supplies services throughout your application.
Project Structure and Program.cs
How a default ASP.NET Core project is laid out and how Program.cs bootstraps the host, services, and request pipeline.
What Is ASP.NET Core?
A cross-platform, open-source, high-performance framework from Microsoft for building web apps, APIs, and microservices on .NET.
Related Reading
Related Study Notes in Microsoft Technologies
Browse all study notesWindows 10 / UWP Development Study Notes
.NET · 30 topics
Microsoft TechnologiesWindows Batch Scripting Study Notes
Batch · 30 topics
Microsoft TechnologiesMFC (Microsoft Foundation Classes) Study Notes
C++ · 30 topics
Microsoft TechnologiesSilverlight Study Notes
.NET · 30 topics
Microsoft TechnologiesXAML Study Notes
.NET · 30 topics
Microsoft TechnologiesWPF (Windows Presentation Foundation) Study Notes
.NET · 30 topics