.NET Core环境变量返回null [英] .NET Core environment variable returns null
问题描述
我有一个.NET Core控制台应用程序.我正在尝试使用以下代码检索环境变量.
var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
但是,变量环境"始终返回null.我通过设置环境变量"ASPNETCORE_ENVIRONMENT"
控制面板->系统属性->环境变量->系统变量
我还尝试使用命令 set ASPNETCORE_ENVIRONMENT = development
设置环境变量,但这也没有用.当我在Visual Studio中调试代码( F5 )时,变量始终返回null.我确保在设置变量或在代码中没有任何空格读取它.我有什么想念的吗?
我认为设置环境变量仅在通过 dotnet run
启动控制台应用程序时有效:
全局设置ASPNETCORE_ENVIRONMENT环境变量时,它使dotnet在以下命令后打开的任何命令窗口中运行均有效值已设置.
来自:
I have a .NET Core console application. I'm trying to retrieve the environment variable using the below code.
var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
However, the variable "environment" always return null. I set the environment variable "ASPNETCORE_ENVIRONMENT" through
Control Panel -> System Properties -> Environment Variables -> System Variables
I also tried setting the environment variable using the command set ASPNETCORE_ENVIRONMENT=development
, but that also did not work. When I debug the code (F5) in Visual Studio, the variable always return null. I have made sure that there aren't any spaces where I set the variable, or in my code where I read it. Is there anything I'm missing?
I think setting the environment variable would only work if you started your console application via dotnet run
:
When the ASPNETCORE_ENVIRONMENT environment variable is set globally, it takes effect for dotnet run in any command window opened after the value is set.
From: Use multiple environments in ASP.NET Core
Try setting it it your Debug properties.
Right click your project in Visual Studio and select Properties → Debug.
Set the environment variable as shown in the image below.
这篇关于.NET Core环境变量返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!