100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
C#

Configuration and Environments

How ASP.NET Core layers configuration sources and switches behavior between Development, Staging, and Production environments.

FoundationsIntermediate9 min readJul 10, 2026
Analogies

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.

csharp
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

Was this page helpful?

Topics covered

#ASPNETCoreStudyNotes#MicrosoftTechnologies#ConfigurationAndEnvironments#Configuration#Environments#IHostEnvironment#Environment#StudyNotes#SkillVeris#ExamPrep