How is Configuration Handled in ASP.NET Core?
π‘ Concept Name
Configuration in ASP.NET Core
π Quick Intro
ASP.NET Core provides a flexible configuration system that reads settings from various sources like appsettings.json, environment variables, and secrets. It uses dependency injection to provide access to these settings throughout the app.
π§ Analogy / Short Story
Think of configuration as ordering food through a delivery app. You specify your preferences (address, cuisine, toppings). The app doesnβt hardcode what you wantβit reads your configuration. ASP.NET Core reads settings similarly, adapting behavior without changing code.
π§ Technical Explanation
Configuration in ASP.NET Core is built on the IConfiguration
interface. It supports:
appsettings.json
andappsettings.{Environment}.json
- Environment Variables
- User Secrets (in development)
- Command-line arguments
- Azure Key Vault, JSON, INI, XML, etc.
You can inject IConfiguration
directly or bind configuration sections to strongly-typed classes using IOptions<T>
.
π― Purpose & Use Case
- β Store connection strings and API keys
- β Manage per-environment settings (dev/staging/prod)
- β Externalize feature toggles or flags
- β Secure sensitive info using secrets or Key Vault
π» Real Code Example
// appsettings.json
{
"AppSettings": {
"Title": "FullStackPrep.Dev"
}
}
// Settings class
public class AppSettings {
public string Title { get; set; }
}
// Program.cs (ASP.NET Core 6+)
builder.Services.Configure<AppSettings>(builder.Configuration.GetSection("AppSettings"));
// Controller
public class HomeController : Controller {
private readonly AppSettings _settings;
public HomeController(IOptions<AppSettings> options) => _settings = options.Value;
public IActionResult Index() => Content($"Title: {_settings.Title}");
}

β Interview Q&A
Q1: What is IConfiguration?
A: Interface to access key-value based configuration data in .NET Core.
Q2: What file commonly stores settings?
A: appsettings.json
Q3: How to bind a config section to a class?
A: Using services.Configure<T>().
Q4: What does IOptions<T> provide?
A: Access to strongly-typed config values via DI.
Q5: Can configuration change per environment?
A: Yes, using appsettings.Development.json, etc.
Q6: What are the config sources in order?
A: Command-line args > Env vars > appsettings.json
Q7: What is User Secrets used for?
A: Store sensitive config in development.
Q8: Can Azure Key Vault be used?
A: Yes, for secure production secrets.
Q9: What is the default config system based on?
A: Microsoft.Extensions.Configuration
Q10: How to read a single config value?
A: Use Configuration["Key"] or GetValue<T>("Key")
π MCQs
Q1. What is IConfiguration in ASP.NET Core?
- A class for logging
- An interface to access config data
- A middleware for caching
- A controller filter
Q2. Which file stores configuration by default?
- web.config
- launchSettings.json
- appsettings.json
- settings.xml
Q3. What does IOptions<AppSettings> provide?
- Logging setup
- Database access
- Strongly-typed config values
- Routing information
Q4. Which config source has highest priority?
- appsettings.json
- Environment variables
- Command-line arguments
- launchSettings.json
Q5. How to register settings class?
- services.Inject<T>()
- services.Register<T>()
- services.Configure<T>()
- services.Map<T>()
Q6. What is the use of User Secrets?
- Store static files
- Store dev-only sensitive info
- Enable logging
- Add middleware
Q7. What namespace contains config system?
- System.Configuration
- Microsoft.Hosting
- Microsoft.Extensions.Configuration
- System.Web
Q8. How to inject configuration in controller?
- Using ViewData
- Using IActionResult
- Using IConfiguration or IOptions
- Using HttpContext
Q9. Which pattern is used for config binding?
- Repository Pattern
- Service Pattern
- Factory Pattern
- Options Pattern
Q10. What method gets single config value?
- GetService<T>()
- UseSetting()
- AddConfig()
- GetValue<T>(key)
π‘ Bonus Insight
You can chain multiple configuration providers for hybrid config (e.g., use appsettings + env vars + Key Vault). Always validate settings using ValidateDataAnnotations()
for critical apps.
π PDF Download
Need a handy summary for your notes? Download this topic as a PDF!