Reading settings from app.config or web.config in .NET
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In classic .NET applications, app.config and web.config are the standard places for environment-specific settings. The important engineering question is not only how to read a value, but how to do it consistently, validate it early, and keep configuration access from spreading as raw magic strings throughout the codebase.
Know Which Section to Use
The two most common configuration sections are appSettings and connectionStrings. appSettings is for simple key-value strings. connectionStrings is for database or service connection strings.
A small configuration file might look like this:
Use appSettings only for small string values. If the data has real structure, a custom section or a newer options-based configuration model is usually better.
Read appSettings with ConfigurationManager
The classic API is ConfigurationManager.AppSettings. It returns strings, so parsing and validation are your responsibility.
Failing fast is important. Silent defaults often turn a simple configuration mistake into a confusing runtime bug later.
Read Connection Strings from the Dedicated Section
Connection strings belong in the connectionStrings section, not mixed into appSettings.
That makes the contract explicit and aligns with the conventions used by .NET tools and older frameworks.
Centralize Configuration Access
A useful improvement is to wrap configuration access in one typed class so the rest of the application stops depending on string literals.
This pattern makes refactoring easier and reduces duplicated parsing code.
web.config Uses the Same Core Model
In older ASP.NET applications, web.config is read through the same ConfigurationManager API. The difference is operational rather than conceptual: web.config changes can recycle the application, and different environments often supply different deployed config files.
If you are maintaining legacy web apps, prefer a small typed wrapper around ConfigurationManager rather than reading XML directly. The framework already knows how to resolve configuration sections correctly.
Common Pitfalls
- Storing structured or sensitive connection information in
appSettingsinstead of the correct section makes configuration harder to manage. - Reading values without validating them turns missing or malformed settings into downstream runtime failures.
- Repeating raw configuration keys throughout the codebase makes renaming and auditing difficult.
- Mixing older
ConfigurationManagerusage with newer configuration styles without a clear boundary creates confusion. - Treating optional and required settings the same way hides which values the app truly depends on.
Summary
- Use
ConfigurationManager.AppSettingsfor simple string settings. - Use
ConfigurationManager.ConnectionStringsfor connection string values. - Parse and validate configuration at the boundary, not deep inside business logic.
- Centralize access behind a typed wrapper instead of scattering magic strings.
- Keep
app.configandweb.configsimple, explicit, and environment-specific.
Related reading
- ReadOnlyCollection or IEnumerable for exposing member collections?
- Recommendation Algorithms for tweets in C
- Recommended Open Source C algorithms data structures libraries
- Reducing memory usage of .NET applications?
- Ref folder within .NET 5.0 bin folder
- Ref in async Task
- Refactoring Backgroundworker to async/await
- Reference a .NET Core Library in a .NET 4.6 project

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack 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.