这是我如何使用环境变量在我的应用 -
在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");
阅读此https://docs.microsoft.com/en-us/aspnet/core/fundamentals/environments –