Access in code: builder.Configuration["ConnectionStrings:Default"]; โ Stored securely under your user profile (not in the project folder). ๐ 5. How do you use environment variables?
Environment variables are great for overriding settings at deployment time (especially in
Docker or Azure).
Example:
set ASPNETCORE_ENVIRONMENT=Production
set
ConnectionStrings__Default="Server=mydb;Database=App;User=sa;Passwor
d=123"
Notice the double underscores __ for nested keys.
Then access in code:
var conn = builder.Configuration["ConnectionStrings:Default"];
๐งญ 6. What are the common environment names?
Follow :
ASP.NET Core defines three common hosting environments:
Environment Purpose
Developmen
Local development, detailed errors, hot reload
Staging Pre-production testing
Production Live environment, performance optimized, no detailed errors
You set the environment via:
set ASPNETCORE_ENVIRONMENT=Development
๐ 7. How do you detect the current environment?
You can inject or access the IWebHostEnvironment or IHostEnvironment service.
Example:
public class HomeController : Controller
private readonly IWebHostEnvironment _env;
public HomeController(IWebHostEnvironment env)
_env = env;
public IActionResult Index()
if (_env.IsDevelopment())
return Content("Running in Development mode");
return Content($"Environment: {_env.EnvironmentName}");
Follow :
You can also access it in Program.cs:
if (builder.Environment.IsProduction())
// Configure production services
๐งพ 8. How to use different appsettings.json files?
ASP.NET Core supports environment-specific JSON files.
Example structure:
appsettings.json
appsettings.Development.json
appsettings.Staging.json
appsettings.Production.json
Program.cs:
builder.Configuration
.AddJsonFile("appsettings.json")
.AddJsonFile($"appsettings.{builder.Environment.EnvironmentName}.jso
n", optional: true);
At runtime, only the file matching the current environment will override base settings.
Example:
appsettings.json
{ "AppName": "MyApp", "LogLevel": "Information" }
appsettings.Production.json
{ "LogLevel": "Error" }
Follow :
Result in Production โ LogLevel = Error.
๐ 9. How to reload configuration dynamically?
You can make JSON configuration files auto-reload when changed.
Example:
builder.Configuration.AddJsonFile("appsettings.json", optional:
false, reloadOnChange: true);
If you update appsettings.json, the new values are reflected automatically in your app
without restarting.
You can subscribe to changes using IOptionsMonitor (see below).
๐ฆ 10. How to bind configuration to POCO classes?
You can map sections of your configuration directly to C# classes (POCOs).
Example:
appsettings.json
"AppSettings": {
"SiteName": "TechStore",
"PageSize": 20,
"EnableCache": true
Create a POCO:
public class AppSettings
Follow :
public string SiteName { get; set; }
public int PageSize { get; set; }
public bool EnableCache { get; set; }
Bind configuration:
builder.Services.Configure<AppSettings>(
builder.Configuration.GetSection("AppSettings"));
Inject it into a controller:
public class HomeController : Controller
private readonly AppSettings _settings;
public HomeController(IOptions<AppSettings> options)
_settings = options.Value;
public IActionResult Index()
return Content($"Welcome to {_settings.SiteName}!");
You can also use IOptionsSnapshot for per-request reload or IOptionsMonitor for live
updates.
Web APIs & REST