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 in CreateDefaultBuilder.
  • Q: How to access strongly typed config in controllers?
    A: Bind a POCO class and inject it using IOptions<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, use IOptionsSnapshot for scoped services.
  • Q: How to secure secrets instead of putting in appsettings?
    A: Use environment variables or Secret Manager.
  • Q: Is appsettings.Production.json automatically used?
    A: Yes, if ASPNETCORE_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: Use GetSection("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!

Learn More About ASP.NET Core ๐Ÿš€

What is ASP.NET Core? ๐Ÿ‘‰ Explained
ASP.NET Core vs .NET Framework ๐Ÿ‘‰ Explained
Role of Kestrel Server in ASP.NET Core ๐Ÿ‘‰ Explained
Middleware & Request Pipeline ๐Ÿ‘‰ Explained
Dependency Injection in ASP.NET Core ๐Ÿ‘‰ Explained
Program.cs vs Startup.cs ๐Ÿ‘‰ Explained
Configuration & appsettings.json ๐Ÿ‘‰ Explained
Environment-specific settings ๐Ÿ‘‰ Explained
Writing Custom Middleware ๐Ÿ‘‰ Explained
Logging in ASP.NET Core ๐Ÿ‘‰ Explained
Static File Middleware ๐Ÿ‘‰ Explained
Routing fundamentals ๐Ÿ‘‰ Explained
Model Binding & Validation ๐Ÿ‘‰ Explained
Razor Pages vs MVC ๐Ÿ‘‰ Explained
Tag Helpers overview ๐Ÿ‘‰ Explained
Filters in MVC (Action, Authorization, Exception) ๐Ÿ‘‰ Explained
Web API controllers & content negotiation ๐Ÿ‘‰ Explained
Versioning ASP.NET Core Web API ๐Ÿ‘‰ Explained
Entity Framework Core introduction ๐Ÿ‘‰ Explained
Code-First vs Database-First in EF Core ๐Ÿ‘‰ Explained
Migrations in EF Core ๐Ÿ‘‰ Explained
LINQ fundamentals ๐Ÿ‘‰ Explained
Async/Await and async controllers ๐Ÿ‘‰ Explained
Error & Exception Handling Middleware ๐Ÿ‘‰ Explained
CORS configuration & usage ๐Ÿ‘‰ Explained
Authentication vs Authorization ๐Ÿ‘‰ Explained
ASP.NET Core Identity basics ๐Ÿ‘‰ Explained
JWT Authentication integration ๐Ÿ‘‰ Explained
Caching strategies ๐Ÿ‘‰ Explained
Session & State Management ๐Ÿ‘‰ Explained
File Upload handling ๐Ÿ‘‰ Explained
Health Checks & monitoring ๐Ÿ‘‰ Explained
Hosted Services & Background Tasks ๐Ÿ‘‰ Explained
Working with IWebHostEnvironment ๐Ÿ‘‰ Explained
IWebHostBuilder and WebHost vs Generic Host ๐Ÿ‘‰ Explained
Deployment to IIS, Kestrel, Nginx, Docker ๐Ÿ‘‰ Explained
Use of HTTP.sys Server ๐Ÿ‘‰ Explained
Configuration providers (JSON, env, CLI) ๐Ÿ‘‰ Explained
Handling Concurrency in EF Core ๐Ÿ‘‰ Explained
Model validation & custom validation ๐Ÿ‘‰ Explained
Dependency Injection service lifetimes ๐Ÿ‘‰ Explained
Security best practices (HTTPS, HSTS, CSP) ๐Ÿ‘‰ Explained
Authorization policies & claims ๐Ÿ‘‰ Explained
Rate limiting & request throttling ๐Ÿ‘‰ Explained
Health & metrics integration ๐Ÿ‘‰ Explained
Swagger/OpenAPI documentation ๐Ÿ‘‰ Explained
Blazor fundamentals ๐Ÿ‘‰ Explained
Razor Class Libraries (RCL) ๐Ÿ‘‰ Explained
SignalR real-time communication ๐Ÿ‘‰ Explained
Performance optimization & profiling ๐Ÿ‘‰ Explained
Share:

Tags:


Feedback Modal Popup