加入收藏 | 设为首页 | 会员中心 | 我要投稿 南平站长网 (https://www.0599zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > Asp教程 > 正文

在 ASP.NET Core 中读取环境变量

发布时间:2022-10-17 07:01:07 所属栏目:Asp教程 来源:未知
导读: 本文介绍了在 ASP.NET Core 中读取环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用 DNX 运行我的 ASP.NET Core 应用程序,

本文介绍了在 ASP.NET Core 中读取环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 DNX 运行我的 ASP.NET Core 应用程序,我能够从命令行设置环境变量,然后像这样运行它:

Running my ASP.NET Core application using DNX, I was able to set environment variables from the command line and then run it like this:

set ASPNET_ENV = Production
dnx web

在 1.0 中使用相同的方法:

Using the same approach in 1.0:

set ASPNETCORE_ENVIRONMENT = Production
dotnet run

不起作用 - 应用程序似乎无法读取环境变量.

does not work - the application does not seem to be able to read environment variables.

Console.WriteLine(Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"));

返回空值

我错过了什么?

推荐答案

您的问题是 = 周围的空格.

Your problem is spaces around =.

这会起作用(在结束引用之前注意空格):

This will work (attention to space before closing quote):

Console.WriteLine(Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT "));

这段代码中ASPNETCORE_ENVIRONMENT后面的空格不是错字!问题中的问题是有额外的空间(在 SET... 中),因此您必须在 GetEnvironmentVariable() 中使用相同的空间.

The space after ASPNETCORE_ENVIRONMENT in this code is not a typo! The problem in the question was having extra space (in SET...), so you must use the same space in GetEnvironmentVariable().

如 ,更好的解决方案是从 SET 命令中完全删除空格:

As noted by Isantipov in a comment, an even better solution is to remove the spaces entirely from the SET command:

set ASPNETCORE_ENVIRONMENT=Production

这篇关于在 ASP.NET Core 中读取环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助ASP 变量,也希望大家多多支持IT屋!

(编辑:南平站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!