这就是我在应用程序中使用环境变量的方式-
在 Visual Studio 中,使用 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"
}
}
}
由于launchSettings.json 仅限于Visual Studio,所以在发布版本的情况下我们使用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>
并且此环境值将使用 -
跨应用程序读取
Environment.GetEnvironmentVariable("MY_TEST");