.NET Core environment variable returns null

你。 提交于 2021-02-08 13:22:15

问题


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?


回答1:


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 PropertiesDebug.

Set the environment variable as shown in the image below.



来源:https://stackoverflow.com/questions/57713484/net-core-environment-variable-returns-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!