ASP.NET Core Configuration & Environments 2 — Questions and Answers
Question 1: What is the purpose of User Secrets in ASP.NET Core development?
- Encrypting production appsettings.json
- Storing sensitive development config outside the project directory (Correct answer)
- Managing user authentication secrets
- Hashing passwords in the database
Correct answer: Storing sensitive development config outside the project directory
User Secrets store sensitive configuration (API keys, connection strings) outside the project folder during development to avoid committing them to source control.
Question 2: Which CLI command initializes User Secrets for an ASP.NET Core project?
- dotnet secret init
- dotnet user-secrets init (Correct answer)
- dotnet secrets create
- dotnet init secrets
Correct answer: dotnet user-secrets init
dotnet user-secrets init adds a UserSecretsId GUID to the project file and sets up the local secrets store.
Question 3: How do environment-specific appsettings files override the base file?
- The environment file completely replaces the base file
- Environment file values override matching keys in appsettings.json; unmatched keys remain from base (Correct answer)
- Only the environment file is loaded in non-Development mode
- Base file values always take precedence
Correct answer: Environment file values override matching keys in appsettings.json; unmatched keys remain from base
appsettings.{Environment}.json merges with appsettings.json; matching keys are overridden while unspecified keys remain from the base file.
Question 4: What format does ASP.NET Core use for nested configuration keys in environment variables?
- Dot notation: Section.Key
- Colon notation: Section:Key or double underscore: Section__Key (Correct answer)
- Slash notation: Section/Key
- Bracket notation: Section[Key]
Correct answer: Colon notation: Section:Key or double underscore: Section__Key
Environment variables use double underscores (__) as hierarchy separators because colons are not valid in all environments, mapping to Section:Key in configuration.
Question 5: What is the IHostEnvironment interface used for?
- Managing hosted services
- Accessing environment name, content root, and web root paths (Correct answer)
- Configuring the host builder
- Managing background tasks
Correct answer: Accessing environment name, content root, and web root paths
IHostEnvironment provides the environment name (Development/Production), ContentRootPath, and other environment metadata.
Question 6: Which method checks if the current environment is Development?
- env.IsMode("Development")
- env.IsDevelopment() (Correct answer)
- env.Environment == "Development"
- env.CheckEnvironment("Development")
Correct answer: env.IsDevelopment()
IHostEnvironment.IsDevelopment() is an extension method that returns true when the environment name equals "Development".
What is the purpose of User Secrets in ASP.NET Core development?