Summary#
Environment is C#'s System.Environment, but an app is not a process on a machine it owns, so it answers with
the app's OWN values. GetEnvironmentVariable reads the variables the app declared — its per-environment config (app.Config)
settings — and the two the platform gives every app; nothing of the host's environment ever reaches an app.
app.Config = [ new Setting("REGION") { Default = "eu-north" } ];
string Region() => Environment.GetEnvironmentVariable("REGION") ?? "unknown";
bool InDevelopment() => Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") == "Development";
string[] Lines(string text) => text.Split(Environment.NewLine);Signature#
string? Environment.GetEnvironmentVariable(string name) // the app's variable, or null when it has none of that name
string Environment.NewLine // "\n"Description#
What a variable is. Each new Setting("NAME") in app.Config is a variable, with the value for the mode the app
is running in — exactly what Config.NAME reads, but by a name only known at run time. The platform adds two:
DOTNET_ENVIRONMENT and ASPNETCORE_ENVIRONMENT, both "Development" on a local platform and "Production" in a
deployment, so the check C# code already makes — GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") == "Development" —
means what it means in ASP.NET. A setting of the same name wins.
What it never is. The host's variables. PATH, HOME and anything the platform process was started with read
null: an app on a shared host has no business seeing them, and an app's code should not change meaning with the
machine it lands on. A secret is not a variable either — read it as Secret.NAME (declaring secrets (app.Secrets)).
Anywhere. The same answer in a server function and in the browser (the values ride the page's boot data, as
Config.X does), and inside a query, where a variable named by the row (Environment.GetEnvironmentVariable(j.Name))
is looked up by the database.
Examples#
[Test]
void Environment_is_the_apps_own() {
Assert.Equal("eu-north", Region());
Assert.True(InDevelopment());
Assert.Null(Environment.GetEnvironmentVariable("PATH"));
Assert.Equal(3, Lines("a\nb\nc").Length);
}See also#
- per-environment config (app.Config) — declare the variables (
app.Config) and read one by name (Config.X) - declaring secrets (app.Secrets) — values that must never reach a browser
- Path — the text of a file path — the text of a file path