liveBook · Manning

Environment variable

As explained in ASP.NET Core in Action, 3e

An environment variable is an operating-system-provided name-value setting that applications can read at runtime.

An environment variable is a name-value setting supplied by the operating system and read by an application at runtime. In ASP.NET Core, environment variables provide configuration without placing values directly in the project folder. WebApplicationBuilder adds the environment-variable configuration provider by default; AddEnvironmentVariables can add it explicitly. section 10.2.3

Use environment variables to:

  • Keep production passwords, connection strings, and API keys out of appsettings.json and source control. section 10.2.3
  • Select the hosting environment with ASPNETCORE_ENVIRONMENT; without it, the application defaults to Production. section 10.4.3
  • Create hierarchical configuration keys using environment-variable names corresponding to configuration sections. section 10.2.3
  • Run the same application with different settings by choosing launchSettings.json profiles or passing an environment at startup. section 10.4.3

Run with a staging environment

Use `dotnet run --no-launch-profile --environment Staging` to start the application in Staging without using a launch profile. For local development, launchSettings.json can assign different environment variables to separate http, https, or IIS Express profiles. section 10.4.3

Figure 10.6 By default, ASP.NET Core applications run in the production hosting environment. You can override this default by setting the ASPNETCORE_ENVIRONMENT variable.
CH10_F06_Lock3

The diagram reinforces the default Production environment and shows that ASPNETCORE_ENVIRONMENT overrides it. section 10.4.3

Environment variables reduce accidental commits, but they are not encrypted: a compromised machine can expose their plain-text values. Use User Secrets locally and consider an encrypted store such as Azure Key Vault for stronger protection; never rely on a private repository as a security boundary. section 10.2.3

How ASP.NET Core in Action, 3e covers it

How deep it goes
Explains provider registration, hosting defaults, local profiles, and security limits, but defers detailed environment-variable naming syntax.
Examples
Uses ASPNETCORE_ENVIRONMENT, launchSettings.json profiles, and dotnet run --environment Staging as concrete configuration examples.
What you can do after
Operate/configure ASP.NET Core applications with production settings, local profiles, and environment-specific behavior.
How it connects
Connects environment variables with configuration providers, appsettings.json, User Secrets, hosting environments, launchSettings.json, and Azure Key Vault.

Also explained in

Compare how 19 books explain environment variable →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of ASP.NET Core in Action, 3e. The section links point to the parts of the book it draws on.