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!

๐Ÿ’ฌ Feedback
๐Ÿš€ Start Learning
Share:

Tags: