仅从web.config读取ASPNETCORE_ENVIRONMENT

时间:2020-11-06 20:31:51

标签: c# asp.net-core

我希望能够在launchSettings中设置我的ASPNETCORE_ENVIRONMENT变量,但不能从那里或系统环境变量中读取。

在代码中,我使用的是Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"),它只返回web.config中的内容。如果我删除该节点,它将返回null

仅适用于web.config。我正在使用<aspNetCore ... hostingModel="inprocess">,这也许是为什么,但是我找不到有关此行为的任何解释或文档。

这在Win10 / IIS(InProcess)上本地运行

Web.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <location path="." inheritInChildApplications="false">
    <system.webServer>
      <handlers>
        <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
      </handlers>
      <aspNetCore processPath="C:\...\my.exe" arguments="" stdoutLogEnabled="false" hostingModel="inprocess">
        <environmentVariables>
          <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development"/>
        </environmentVariables>
      </aspNetCore>
    </system.webServer>
  </location>
</configuration>

launchSettings.json

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iis": {
      "applicationUrl": "https://www.mytestsite.com",
      "sslPort": 443
    }
  },
  "$schema": "http://json.schemastore.org/launchsettings.json",
  "profiles": {
    "IIS": {
      "commandName": "IIS", 
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "test"
      }
    }
  }
}

0 个答案:

没有答案