.NET Configuration
app.config
web.config
settings.settings
.NET Framework

.NET Configuration app.config/web.config/settings.settings

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

Legacy .NET applications often mix values across app.config, web.config, and settings.settings, which creates confusion about where configuration should live. A clear boundary between static application settings and environment-specific secrets makes maintenance much easier.

For modern .NET code, the preferred model is unified configuration with layered sources and strongly typed options. For older frameworks, the same principle applies even if the file names differ.

The goal is to make configuration predictable, testable, and safe to override per environment without manual file edits during deployment.

Core Sections

Understand the failure mode

Quick fixes usually solve the visible symptom and skip the reason the behavior appears. That makes the same issue return in another environment. Start by identifying the exact boundary where data format, lifecycle timing, or control flow changes.

Write one input and one expected output before modifying implementation details. This converts debugging into a deterministic process and creates a clear contract for reviewers.

Apply a repeatable implementation pattern

Good implementation is not only about passing the current test. It should also establish a shape that future contributors can follow without guessing hidden assumptions. Keep configuration explicit, avoid hidden global state, and isolate side effects from pure logic.

csharp
1using System;
2using System.Configuration;
3
4class Program
5{
6    static void Main()
7    {
8        string apiUrl = ConfigurationManager.AppSettings["ApiUrl"];
9        Console.WriteLine($"ApiUrl: {apiUrl}");
10    }
11}

This baseline example is intentionally concise and runnable. In production systems, keep the same structure and move environment-specific values into configuration sources.

Validate with a smoke test

After coding, run a short smoke test that covers the critical path end to end. Smoke checks provide fast confidence and catch integration issues early. Follow with targeted failure cases that represent the most likely operational mistakes.

csharp
1public class MailOptions
2{
3    public string Host { get; set; } = "";
4    public int Port { get; set; }
5}
6
7// In startup configuration
8// services.Configure<MailOptions>(configuration.GetSection("Mail"));

Run validation locally and in continuous integration using the same command shape. Consistent execution paths reduce drift and prevent merge-time surprises.

Hardening for production use

After functional correctness, improve observability and failure clarity. Include enough context in logs to diagnose incidents quickly, such as relevant identifiers, endpoint details, or version metadata. Prefer explicit failure paths instead of silent fallback behavior.

Document assumptions near the code, including environment dependencies, resource limits, or format expectations. Explicit assumptions make upgrades safer and reduce review time.

Maintenance and regression strategy

Every resolved bug should add a regression test that would fail before the fix and pass after it. Keep tests compact, deterministic, and easy to run in local development.

When new requirements arrive, extend the same structure instead of adding one-off conditionals. Consistent structure prevents complexity spikes and keeps long-term maintenance predictable.

Environment override strategy

Define which settings can differ by environment and which must remain constant across all deployments. Keep secrets in secure stores and inject them at runtime, while keeping defaults in version-controlled configuration files. This separation simplifies audits, supports safer rotation workflows, and reduces the chance of accidental secret exposure in source control.

Operationally, include a startup diagnostic that logs which configuration providers were loaded and whether required keys are present. This gives immediate feedback during deployment and avoids hidden misconfiguration.

Common Pitfalls

  • Duplicating the same setting in multiple config files creates ambiguity.
  • Storing secrets directly in config files increases leak risk.
  • Using string literals for keys everywhere makes refactoring error-prone.
  • Failing to validate required settings causes runtime failures later.
  • Manual config edits on servers reduce deployment repeatability.

Summary

  • Define one authoritative source for each configuration value.
  • Separate application defaults from environment overrides.
  • Bind critical settings to typed option classes.
  • Validate required configuration during startup.
  • Use deployment pipelines to manage per-environment values safely.

Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.