2

从命令行,如果我运行set ASPNETCORE_ENVIRONMENT=Development && dotnet watch run,我的托管环境设置为开发。

但是,如果我在 project.json 文件中添加与命令相同的行,则观察程序的托管环境始终是生产环境:

"commands": {
  "watch": "set ASPNETCORE_ENVIRONMENT=Development && dotnet watch run"
},

是否有任何参数可以传递给 dotnet run 以进行托管环境开发?我确实需要这个作为命令工作。

4

2 回答 2

3

您可以添加Microsoft.Extensions.Configuration.CommandLine从命令行读取配置的包:

public class Program
{
    public static void Main(string[] args)
    {
        var config = new ConfigurationBuilder()
            .AddEnvironmentVariables()
            .AddCommandLine(args)
            .Build();

        var host = new WebHostBuilder()
            .UseEnvironment(config["ASPNETCORE_ENVIRONMENT"])
            .UseKestrel()
            .UseContentRoot(Directory.GetCurrentDirectory())
            .UseIISIntegration()
            .UseStartup<Startup>()
            .Build();

        host.Run();
    }
}

dotnet run可以这样做:

dotnet run --ASPNETCORE_ENVIRONMENT Development

你也应该能够做类似的dotnet watch run事情。

于 2016-07-08T19:16:44.077 回答
0

我终于想通了!

诀窍是右键单击项目,转到属性,然后选择调试选项卡。接下来,在 Profile 下,我选择了我的 projects.json 中定义的命令的名称:“watch”。选择后,我单击按环境变量添加,并添加名称/值对 ASPNETCORE_ENVIRONMENT 和 Development。

在此处输入图像描述

这实际上是在解决方案资源管理器的属性下上传 launchSettings.json 文件。该文件也可以手动编辑。输出看起来像这样:

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:56846/",
      "sslPort": 0
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "LendingTree.CreditCards.Web": {
      "commandName": "Project",
      "launchBrowser": true,
      "launchUrl": "http://localhost:5000",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "Watcher Development": {
      "commandName": "Watcher Development",
      "launchBrowser": true,
      "launchUrl": "http://localhost:5000",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}
于 2016-07-16T01:46:13.937 回答