指定 asp.net 核心 1.0 WebAPI.exe 应在 program.cs 中用于生产和开发的 url(端口)

Specifying the url (port) that a asp.net core 1.0 WebAPI.exe should use in program.cs for both prod and dev

我在我的 asp.net core 1.0 web api (.NET Framework) program.cs 中执行以下操作以指定我希望我的 web api exe 连接到哪个端口运行 仅用于开发目的:

public static void Main(string[] args)
{
    var host = new WebHostBuilder()
        .UseKestrel()
        .UseContentRoot(Directory.GetCurrentDirectory())
        .UseIISIntegration()
        .UseStartup<Startup>()
        .UseUrls(new string[1] { "http://*:12012" })
        .Build();

    host.Run();
}

但是,当我发布到生产环境时,此行会导致 WebAPI 出错,因为我希望 exe 使用生产网络-api url 即 productionWeb/api/values 而不是 localhost:12012/值

无论如何,我是否可以两全其美,能够指定我希望它在端口 12012 上 运行 用于开发目的,而生产 url 用于生产目的?

我目前的解决方案是在发布之前注释掉该行。

使用 IIS 时,您将覆盖 url IIS(AspNet 核心模块)告诉应用程序通过在 .UseIISIntegration() 之后调用 .UseUrls() 来收听。您应该更改这两个调用的顺序,使 .UseIISIntegration().UseUrl() 之后。如果您不是 运行 IIS,.UseIISIntegration() 将不会触及您设置的 urls,因此在开发中您的应用程序仍将侦听端口 12012。当 运行 IIS .UseIISIntegration() 将覆盖 url 以侦听 IIS 告诉它侦听的端口。我写了一个 post on running Asp.NET Core apps with IIS and Azure Websites 来解释事情是如何工作的,包括这种细微差别。