skip to content

Environments & Launch Profiles

ASPNETCORE_ENVIRONMENT versus DOTNET_ENVIRONMENT, IWebHostEnvironment checks such as IsDevelopment, and launchSettings.json profiles. Interviewers ask why an app runs as Production on a server.

part ofASP.NET Coreread it →
on this pageshow

explore

questions

5

In ASP.NET Core 10, which environment does an app run in when nothing sets one, and where does Development come from locally?

level: juniorimportance: must knowfreq 76%

answer

  1. a safe default, not a convenient one
  2. Environments.Production when the key is missing
  3. Properties/launchSettings.json sets Development
  4. ASPNETCORE_ENVIRONMENT in the server's process
  5. Hosting environment: line at startup

basics

~20 s

An ASP.NET Core 10 app runs as Production unless something supplies an environment name. Locally, Development comes from the launch profiles in Properties/launchSettings.json, which dotnet run and the IDEs apply but which are never deployed.

solid answer

~40 s

At startup the host reads the `environment` key from its host configuration, which is fed by `ASPNETCORE_ENVIRONMENT`, `DOTNET_ENVIRONMENT`, an `--environment` command-line argument or `WebApplicationOptions.EnvironmentName`; when none of them supplies a value it falls back to `Environments.Production`. On a laptop the app reports Development only because the template's `Properties/launchSettings.json` profiles set `ASPNETCORE_ENVIRONMENT` to `Development`, and `dotnet run`, Visual Studio and VS Code apply the profile. The Web SDK never copies that file to the build or publish output, so on a server where nothing else sets the variable, `dotnet MyApp.dll` runs as Production. To run as Staging you set the variable or argument in the server's own process environment (a service unit, a container's environment, IIS `web.config`) and confirm it in the `Hosting environment:` line logged at startup.

code

json · 13 lines
json
{
  "profiles": {
    "https": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": false,
      "applicationUrl": "https://localhost:7129;http://localhost:5087",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

go deeper

for a junior

Recall that the default is Production and that Development on your machine comes from the launch profile, not from the app.

for a middle

Explain the host configuration sources that feed the environment key and why launchSettings.json never reaches bin or publish output.

for a senior

Show how you set and verify the environment per host type, and why failing closed to Production is the right default for an unconfigured server.

for a principal

Argue for making the environment an explicit, audited deployment input rather than something each team sets ad hoc on each server.

## What an environment is in ASP.NET Core An **environment** in ASP.NET Core is a single string, exposed as `IWebHostEnvironment.EnvironmentName`, that the host resolves once while the app starts. The framework ships three well-known names as constants on `Microsoft.Extensions.Hosting.Environments`: `Development`, `Staging` and `Production`. Any other string is also legal. The name matters because the framework and your own code branch on it: `WebApplication` adds the developer exception page only in Development, the host switches on DI scope validation only in Development, and the name selects which `appsettings.{Environment}.json` file is layered over `appsettings.json`. ## How the host picks the name The name comes from the `environment` key of **host configuration**, which `WebApplication.CreateBuilder` assembles before any app settings file is read: 1. Environment variables with the `ASPNETCORE_` prefix, so `ASPNETCORE_ENVIRONMENT` becomes the key `environment`. 2. Environment variables with the `DOTNET_` prefix, so `DOTNET_ENVIRONMENT` maps to the same key. 3. Command-line arguments, so `--environment Staging` maps to it too. 4. `WebApplicationOptions.EnvironmentName`, if code passed one to `CreateBuilder`. If none of these supplies a value, the hosting environment is created with `Environments.Production`. There is no fallback to Development: **an unconfigured app is a Production app**. ## Why the laptop says Development The `web`, `webapi`, `mvc`, `razor` and `blazor` templates write `Properties/launchSettings.json` with an `http` and an `https` profile (`webapiaot` writes only `http`). Each profile has `"commandName": "Project"` and an `environmentVariables` block setting `ASPNETCORE_ENVIRONMENT` to `Development`. When you press F5 in Visual Studio, start the app from VS Code with C# Dev Kit, or type `dotnet run`, the tool reads that file, picks a profile and exports its variables into the new process. The app itself never opens the file; it simply finds `ASPNETCORE_ENVIRONMENT=Development` in its environment. ## Why the server says Production | Where the app starts | What sets the name | Result | |---|---|---| | `dotnet run` on a laptop | the launch profile's `environmentVariables` | Development | | `dotnet MyApp.dll` on a server, nothing configured | nothing | Production | | A service with `ASPNETCORE_ENVIRONMENT=Staging` in its environment | the variable | Staging | | `dotnet MyApp.dll --environment Staging` | the command-line argument | Staging | The Web SDK adds `Properties/launchSettings*.json` as `None` items with `CopyToOutputDirectory` and `CopyToPublishDirectory` set to `Never`, so the file is in neither `bin/` nor the publish folder. Even if someone copied it across by hand, `dotnet MyApp.dll` and a framework-dependent executable do not read launch profiles; only `dotnet run` and the IDEs do. ## Setting it on a real server - **A Linux service**: put `ASPNETCORE_ENVIRONMENT=Staging` in the service definition's environment, not in an interactive shell. - **A container**: pass it as a runtime environment variable or in the compose file's environment section. - **IIS**: set it in the `<environmentVariables>` element of `web.config`, or set the `EnvironmentName` MSBuild property so `dotnet publish` writes it there. - **Any host**: append `--environment Staging` to the command that starts the app. Then read the startup log. The `Microsoft.Hosting.Lifetime` category logs `Hosting environment: Staging` right after `Application started`, which is the value the framework actually used. ```bash ASPNETCORE_ENVIRONMENT=Staging dotnet FerryBooking.dll dotnet FerryBooking.dll --environment Staging ``` ## What the name switches inside the framework The environment is not just a label in a log line. When the name is Development, ASP.NET Core 10 changes several defaults at once: 1. `WebApplication` inserts the developer exception page at the start of the pipeline. 2. The default service provider is built with `ValidateScopes` and `ValidateOnBuild` turned on, so captive scoped services and missing registrations fail fast. 3. User secrets are added to configuration for the entry assembly. 4. `appsettings.Development.json` is layered over `appsettings.json`. Under Production only the last kind of step happens, with `appsettings.Production.json` if it exists. That is why the same build behaves so differently on a laptop and on a server: the code is identical, but the environment name turned a set of developer conveniences on locally and left them off on the server. Teams that only ever test with `dotnet run` never see the Production behaviour until the first deployment. ## Why Production is the right default A server that nobody configured should fail **closed**: no stack traces in responses, no dev-only diagnostics, no user secrets. Defaulting to Production gives exactly that, at the cost of surprising developers who expected their laptop's behaviour on the server. The fix is never to change the default; it is to set the environment explicitly wherever the app is deployed.

  • Does starting as Production change anything beyond the name?
    Yes. WebApplication adds the developer exception page only when IsDevelopment() is true, the host enables ValidateScopes and ValidateOnBuild only in Development, and user secrets are loaded only in Development. An unconfigured server therefore hides exception details, skips the extra DI checks and ignores user secrets, which is the safe outcome.
  • Can a key in appsettings.json set the environment?
    No. The environment is resolved from host configuration while the builder is created, before appsettings.json is added; the name is what chooses which appsettings.{Environment}.json file loads. An ASPNETCORE_ENVIRONMENT entry inside appsettings.json is an ordinary configuration key that nothing treats as the environment.

  • With no variable set, ASP.NET Core falls back to Development.
  • launchSettings.json is published with the app and sets its environment on the server.
  • Adding ASPNETCORE_ENVIRONMENT to appsettings.json switches the environment.
  • The build configuration, Debug or Release, decides the hosting environment.
open as a page

In ASP.NET Core 10, what does IWebHostEnvironment expose, and how do IsDevelopment and IsEnvironment treat a custom environment name such as QA?

level: middleimportance: should knowfreq 55%

basics

~20 s

IWebHostEnvironment exposes the environment name, application name, content root and web root (wwwroot by default). IsDevelopment, IsStaging, IsProduction and IsEnvironment compare the name case-insensitively, and a custom name like QA matches none of the three built-ins.

open as a page

In an ASP.NET Core 10 project, what do the profiles in Properties/launchSettings.json configure, which tools apply them, and why don't they reach production?

level: middleimportance: should knowfreq 50%

basics

~20 s

launchSettings.json holds named launch profiles (environment variables, application URLs, browser options) that dotnet run, Visual Studio and VS Code apply when starting the app locally. The Web SDK keeps it out of build and publish output, so servers never see it.

open as a page

In ASP.NET Core 10, when DOTNET_ENVIRONMENT and ASPNETCORE_ENVIRONMENT disagree, which one sets the environment under WebApplication.CreateBuilder, and why?

level: middleimportance: should knowfreq 42%

basics

~10 s

Under WebApplication.CreateBuilder in ASP.NET Core 10, DOTNET_ENVIRONMENT wins. Both prefixes map to the same environment key, the DOTNET_ source is added after the ASPNETCORE_ one, and configuration returns the value from the last source.

open as a page

A ferry-booking site on ASP.NET Core 10 logs 'Hosting environment: Production' on its staging server although ops set ASPNETCORE_ENVIRONMENT=Staging; how do you find out why?

level: seniorimportance: should knowfreq 30%

basics

~20 s

Check what the running process actually received. The variable may sit in the wrong scope (an SSH shell instead of the service or container), be outranked by DOTNET_ENVIRONMENT, --environment or WebApplicationOptions, or be lost when web.config is republished.

open as a page