Configuration & appsettings.json in ASP.NET Core
๐ก Concept Name
Configuration & appsettings.json in ASP.NET Core
๐ Quick Intro
ASP.NET Core uses a flexible configuration system that supports hierarchical key-value pairs from multiple sources like appsettings.json
, environment variables, and command-line arguments.
๐ง Analogy / Short Story
Think of configuration like a personal assistant managing your preferences: one assistant reads your written notes (appsettings.json
), another listens to your voice (environment variables), and another reads sticky notes you place (CLI). Whichever speaks last, wins.
๐ง Technical Explanation
ASP.NET Core builds configuration using the ConfigurationBuilder
. It reads from sources in order and stores them as key-value pairs, which you can inject using the IConfiguration
interface.
- appsettings.json: Default file for settings.
- appsettings.{Environment}.json: Overrides for specific environments.
- Environment Variables: Useful in Docker/Kubernetes.
- CLI Args: Highest priority if used.
๐ฏ Purpose & Use Case
- โ Store app configuration values
- โ Use different settings per environment
- โ Read connection strings and feature toggles
- โ Secure secrets via external providers
- โ Simplify config management in DevOps pipelines
๐ป Real Code Example
Sample appsettings.json:
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning"
}
},
"ConnectionStrings": {
"DefaultConnection": "Server=.;Database=AppDb;Trusted_Connection=True;"
}
}
Reading values in C# (Program.cs or controller):
var config = builder.Configuration;
var connectionString = config.GetConnectionString("DefaultConnection");
var logLevel = config["Logging:LogLevel:Default"];

๐ฏ Interview Q&A
- Q: What is
IConfiguration
in ASP.NET Core?
A: It provides a way to read config values from multiple sources like JSON, ENV, CLI, etc. - Q: How does ASP.NET Core load
appsettings.json
?
A: It is added by default to the configuration pipeline inCreateDefaultBuilder
. - Q: How to access strongly typed config in controllers?
A: Bind a POCO class and inject it usingIOptions<T>
. - Q: What happens if multiple config sources have the same key?
A: Last source added overrides previous ones (later wins). - Q: Can you change config without restarting the app?
A: Yes, useIOptionsSnapshot
for scoped services. - Q: How to secure secrets instead of putting in appsettings?
A: Use environment variables orSecret Manager
. - Q: Is
appsettings.Production.json
automatically used?
A: Yes, ifASPNETCORE_ENVIRONMENT
is set to "Production". - Q: What is
ConfigureServices
method used for?
A: It registers services and configuration logic into DI. - Q: How to bind nested sections in config?
A: UseGetSection("Section")
multiple times or bind whole object. - Q: What is the use of
AddJsonFile
method?
A: It explicitly adds JSON config files to the configuration builder.
๐ MCQs
๐ MCQs
Q1. What interface is used to access configuration in ASP.NET Core?
- IConfig
- IOptions
- IConfiguration
- IAppSettings
Q2. What file format is used for default config in ASP.NET Core?
- XML
- YAML
- JSON
- INI
Q3. How to access settings in a strongly typed way?
- IValue
- IAppConfig
- IOptions
- IBind
Q4. What happens if ENV and appsettings.json have the same key?
- JSON wins
- ENV value wins
- Both fail
- It throws error
Q5. Which method loads appsettings.json by default?
- ConfigureBuilder
- AddConfig
- CreateDefaultBuilder
- UseDefaults
Q6. Which config source is used for command line arguments?
- EnvConfig
- CommandLine
- RuntimeArgs
- ConsoleReader
Q7. How can secrets be securely stored?
- appsettings.json
- public.json
- Environment.cs
- Secret Manager
Q8. Which method binds config section to POCO?
- UseConfig()
- GetValue()
- GetSection().Bind()
- MapConfig()
Q9. What is the use of IOptionsSnapshot?
- Singleton access
- Scoped config updates
- Logging
- Debugging
Q10. How to use different config for prod & dev?
- Multiple apps
- Rewrite settings
- Environment-specific JSON files
- Change app.cs
๐ PDF Download
Need a handy summary for your notes? Download this topic as a PDF!