ASP.NET Core托管环境变量被忽略 [英] ASP.NET Core hosting environment variable ignored
问题描述
我的登台服务器上有两个网站,它们都是在IIS中运行的ASP.NET Core站点.我已将环境变量ASPNETCORE_ENVIRONMENT
设置为计算机范围内的Staging
.这对于其中一个站点来说效果很好,但是另一个站点会忽略该变量,而是在生产模式下运行.我必须将托管环境配置为web.config
文件,才能在暂存模式下运行它.
I have two web sites on my staging server, and both are ASP.NET Core sites that run in IIS. I have set the environment variable ASPNETCORE_ENVIRONMENT
to Staging
machine-wide. This works well for one of the sites, however the other ignores the variable and runs in production mode instead. I have to configure the hosting environment into the web.config
file to run it in staging mode.
为什么一个站点不考虑环境变量?
Why does one site not take the environment variable into account?
在我的两个Startup(IHostingEnvironment env)
构造函数中,我都使用环境变量:
In both of my Startup(IHostingEnvironment env)
constructors, I use the environment variables:
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddJsonFile("logging.json")
.AddEnvironmentVariables(); // <---
Configuration = builder.Build();
}
推荐答案
如这个类似的问题中所述,窍门仅仅是设置应用程序池以加载用户变量(IIS->服务器->应用程序池->右键单击池->设置应用程序池默认值...->加载用户配置文件= True).
As said in this similar question, the trick was simply to set the app pool to load the user variables (IIS -> Server -> App Pools -> Right click on pool -> Set application pool defaults... -> Load User Profile = True).
我仅相应地配置了一个应用程序池,因此只有一个站点可以访问环境变量.
I configured only one of my app pools accordingly, thus only one of the sites could access the environment variables.
这篇关于ASP.NET Core托管环境变量被忽略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!