This is how I am using Environment variables in my applications-
In Visual Studio, using launchSettings.json-
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"MY_TEST":"123"
}
},
"SamplePractice": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"MY_TEST":"123"
}
}
}
Since launchSettings.json is only limited to Visual Studio, In case of publish version we use web.config-
<aspNetCore processPath="dotnet" arguments=".\MyAspNetCoreApplication.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false" >
<environmentVariables>
<environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" />
<environmentVariable name="MY_TEST" value="123" />
</environmentVariables>
</aspNetCore>
And this environment value will read across application using -
Environment.GetEnvironmentVariable("MY_TEST");