Startup.cs返回错误的环境

dat*_*kom 5 c# asp.net-core-mvc .net-core asp.net-core asp.net-core-1.0

启动类包含

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);

    Console.WriteLine($"{env.EnvironmentName.ToString()}");

    if (env.IsDevelopment())
    {
        // For more details on using the user secret store see 
        // https://go.microsoft.com/fwlink/?LinkID=532709
        builder.AddUserSecrets();
    }

    builder.AddEnvironmentVariables();
    Configuration = builder.Build();
}
Run Code Online (Sandbox Code Playgroud)

但是env.EnvironmentName.ToString()返回“生产”。

我已经在launchSettings.json中将ASPNETCORE_ENVIRONMENT设置为“开发”

San*_*ket 6

当您也设置了环境时,通常会发生这种情况web.config

例如,如果您的环境设置Production如下launchSettings.json-

  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Production"
      }
    },
Run Code Online (Sandbox Code Playgroud)

在 中web.config,如果你有其他环境Staging-

<aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false">
  <environmentVariables>
    <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Staging" />
  </environmentVariables>
</aspNetCore>
Run Code Online (Sandbox Code Playgroud)

Staging在这种情况下,当您尝试读env.EnvironmentName入时您会得到startup.cs

看看这是否有帮助。


dat*_*kom 1

使用 powershell 命令得到这个工作:

$Env:ASPNETCORE_ENVIRONMENT = "Development"