What are environments in ASP.NET Core?
Quick Answer
An environment is a named runtime context — Development, Staging, Production (or your own) — set by the ASPNETCORE_ENVIRONMENT variable. It controls which appsettings.{Environment}.json loads and lets code branch: show the developer exception page in Development, a friendly error page in Production.
An environment is just a named context — Development, Staging, Production — driven by the ASPNETCORE_ENVIRONMENT variable. It picks the environment-specific appsettings file to layer on top, and it lets Program.cs branch: developer exception page and Swagger in Development, the friendly error handler and HSTS in Production. If it's not set, the app assumes Production, which is the safe default. I keep those checks in the pipeline setup and use configuration or feature flags for anything in business logic.
- Set via
ASPNETCORE_ENVIRONMENT(env var) orlaunchSettings.jsonlocally. Defaults toProductionif unset. app.Environment.IsDevelopment()/IsProduction()/IsEnvironment("QA").appsettings.Development.jsonlayers overappsettings.json.- Common branches: developer exception page vs
UseExceptionHandler; verbose logging vs warnings-only; Swagger UI in non-Production only; HSTS in Production.
Keep environment checks in Program.cs for pipeline setup; avoid scattering if (env.IsDevelopment()) through business code — use configuration/feature flags for that.