In ASP.NET Core 10, which environment does an app run in when nothing sets one, and where does Development come from locally?
answer
- a safe default, not a convenient one
- Environments.Production when the key is missing
- Properties/launchSettings.json sets Development
- ASPNETCORE_ENVIRONMENT in the server's process
- Hosting environment: line at startup
basics
~20 sAn 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 sAt 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{
"profiles": {
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": false,
"applicationUrl": "https://localhost:7129;http://localhost:5087",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}go deeper
Recall that the default is Production and that Development on your machine comes from the launch profile, not from the app.
Explain the host configuration sources that feed the environment key and why launchSettings.json never reaches bin or publish output.
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.
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.