C# 为什么在我将ASP.NET核心web应用程序部署到Azure时未检测到我的环境设置?

C# 为什么在我将ASP.NET核心web应用程序部署到Azure时未检测到我的环境设置?,c#,azure,asp.net-core,environment-variables,C#,Azure,Asp.net Core,Environment Variables,我试图在我的web应用程序中定义一些环境变量。我在Azure上托管我的站点,该站点有一个登台部署槽,该槽是站点首先进入的,然后,如果更改被注销,它将发布到生产环境中 这两个位置的URL为: 生产 "BaseUrl": "https://mycompany.azurewebsites.net" 登台 "BaseUrl": "https://mycompany-staging2n1h.azurewebsites.net"

我试图在我的web应用程序中定义一些环境变量。我在Azure上托管我的站点,该站点有一个登台部署槽,该槽是站点首先进入的,然后,如果更改被注销,它将发布到生产环境中

这两个位置的URL为:

生产

"BaseUrl": "https://mycompany.azurewebsites.net"
登台

"BaseUrl": "https://mycompany-staging2n1h.azurewebsites.net"
我希望我的
appsettings.Staging.json
文件在站点到达Staging区域时使用,然后一旦发布到生产环境中,我希望它使用
appsettings.json

我的理解是,如果你不定义生产 应用程序中的appsettings.Production.json文件默认为
appsettings.json

因此,我在应用程序中创建了一个
appsettings.Staging.json
文件,其中包含与生产环境不同的连接字符串

appsettings.json

appsettings.Staging.json

然后我进入
launchSettings.json
文件进行如下设置:

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:xxxx",
      "sslPort": xxxx
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "MyCompany": {
      "commandName": "Project",
      "launchBrowser": true,
      "environmentVariables": { 
        "ASPNETCORE_ENVIRONMENT": "Development"
      },
      "applicationUrl": "https://localhost:5001;http://localhost:5000"
    },
    "MyCompany Staging": {
      "commandName": "Project",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Staging"
      }
    }
  }
}

当我将我的应用程序部署到Azure时,它进入了暂存区域,它看到了错误的连接字符串,这意味着它没有使用正确的appsettings文件。我是否错过了一个步骤或设置不正确?

您需要在Azure上设置
ASPNETCORE\u环境
-对于应用程序服务,它位于配置部分:

(对
launchSettings.json
的更改在部署应用程序时无效,它仅由Visual Studio使用)

正如政府所说:

要确定运行时环境,ASP.NET Core将读取以下环境变量:

  • DOTNET\u环境
  • ASPNETCORE\u环境
    调用ConfigureWebHostDefaults时。默认ASP.NET核心web应用程序模板调用ConfigureWebHostDefaults。
    ASPNETCORE\u环境
    值覆盖
    DOTNET\u环境
  • 框架提供了以下值:

    • 开发:launchSettings.json文件将
      ASPNETCORE_环境
      设置为在本地计算机上进行开发
    • 登台
    • 生产:如果未设置
      DOTNET_环境
      ASPNETCORE_环境
      ,则为默认值

    当然由您决定,但我发现使用应用程序服务配置更容易。这些变量作为环境变量拉入,将覆盖json文件中的任何内容。
    {
      "AzureAd": {
        "CallbackPath": "/signin-oidc",
        "BaseUrl": "https://mycompany-staging2n1h.azurewebsites.net"
      },     
      "ConnectionStrings": {
        "MyCompanyConnection": "staging connection string"
      },
      "AllowedHosts": "*"
    }
    
    {
      "iisSettings": {
        "windowsAuthentication": false,
        "anonymousAuthentication": true,
        "iisExpress": {
          "applicationUrl": "http://localhost:xxxx",
          "sslPort": xxxx
        }
      },
      "profiles": {
        "IIS Express": {
          "commandName": "IISExpress",
          "launchBrowser": true,
          "environmentVariables": {
            "ASPNETCORE_ENVIRONMENT": "Development"
          }
        },
        "MyCompany": {
          "commandName": "Project",
          "launchBrowser": true,
          "environmentVariables": { 
            "ASPNETCORE_ENVIRONMENT": "Development"
          },
          "applicationUrl": "https://localhost:5001;http://localhost:5000"
        },
        "MyCompany Staging": {
          "commandName": "Project",
          "launchBrowser": true,
          "environmentVariables": {
            "ASPNETCORE_ENVIRONMENT": "Staging"
          }
        }
      }
    }