how to get value from appsettings.json
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In .NET Core and .NET 5+, appsettings.json replaces the older web.config and app.config XML files for application configuration. The framework provides a built-in configuration system that reads appsettings.json at startup and makes values available through dependency injection. You access values using IConfiguration for raw key-value lookups or the strongly-typed Options pattern for structured settings.
The appsettings.json File
Method 1: IConfiguration (Direct Access)
Inject IConfiguration and read values using section-colon-key notation:
The colon : separates nested sections. GetValue<T> handles type conversion automatically.
Method 2: Options Pattern (Recommended)
Bind a configuration section to a strongly-typed class:
Method 3: Bind to a Class Manually
Environment-Specific Settings
.NET automatically loads environment-specific overrides:
The environment is determined by the ASPNETCORE_ENVIRONMENT environment variable. Later files override earlier ones — only the keys you specify are overridden, not the entire section.
Reading Arrays
IOptionsSnapshot vs IOptionsMonitor
Use IOptionsSnapshot when you want updated values per request without restarting. Use IOptionsMonitor for long-running services that need to react to config changes.
Reading in Program.cs (Before DI)
Common Pitfalls
- Null values:
_config["NonExistent:Key"]returnsnull, not an exception. Always check for null or useGetValue<T>("key", defaultValue)with a fallback. - Case sensitivity: Configuration keys are case-insensitive in .NET.
_config["appsettings:apikey"]works the same as_config["AppSettings:ApiKey"]. - Secrets in appsettings.json: Never store passwords or API keys in
appsettings.jsonfor production. Use User Secrets (dotnet user-secrets), environment variables, or Azure Key Vault instead. - Forgetting to register Options:
IOptions<T>injection fails with a DI error if you forgetbuilder.Services.Configure<T>(section)in Program.cs. - IOptions is singleton:
IOptions<T>reads the value once and caches it forever. If you editappsettings.jsonat runtime, useIOptionsSnapshot<T>orIOptionsMonitor<T>to pick up changes.
Summary
- Use
IConfiguration["Section:Key"]for quick, one-off value access - Use the Options pattern (
IOptions<T>) for structured, type-safe settings - Connection strings have a shortcut:
_config.GetConnectionString("Name") - Environment-specific files (
appsettings.{Environment}.json) override base settings automatically - Use
IOptionsSnapshot<T>for per-request reload orIOptionsMonitor<T>for change notifications - Never store secrets in
appsettings.json— use User Secrets or environment variables
Related reading
- How to get Xml as string from XDocument?
- How to group by multiple columns using LINQ
- How to group by multiple columns using LINQ
- How to handle AccessViolationException
- How to handle async Start errors in TopShelf
- How to handle click event in Button Column in Datagridview?
- How to handle Task.Run Exception
- How to hide public methods from IntelliSense

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.