Osy#betaRun it on Osyrin: osyrin.com

Reference / Stdlib

Environment — the app's own variables

Environment.GetEnvironmentVariable(name) · Environment.NewLine

C#'s `System.Environment`, answering with the APP's values and never the host's: `GetEnvironmentVariable(name)` reads the app's own `app.Config` settings for the mode it runs in, plus `DOTNET_ENVIRONMENT` / `ASPNETCORE_ENVIRONMENT` ("Development" or "Production"); any other name is null. `Environment.NewLine` is "\n". In a server function, in the browser and inside a query alike.

stable2 examples compiled by CIenvironmentconfigbcl

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#

Related

per-environment config (app.Config)

`app.Config` declares your app's per-environment settings — values that differ between development and production, like…

declaring secrets (app.Secrets)

`app.Secrets` declares the named secrets your app uses — API keys, tokens, client secrets. Each is `new…

Path — the text of a file path

C#'s `System.IO.Path`: build and take apart file paths as text — `Path.Combine`, `GetFileName`…