【问题标题】:localhost port is changing in Visual Studio 2017localhost 端口在 Visual Studio 2017 中发生变化
【发布时间】:2018-03-14 19:19:03
【问题描述】:

我们有一个使用 ASP.NET Core 和 Visual Studio 2017 制作的 API 应用程序。我们有 4 名开发人员在这个项目中工作,有时项目的端口会发生变化,而我们任何人都没有做此更改。

这是应用程序的调试配置:

有人遇到过这种情况吗?

【问题讨论】:

  • 如果您是源代码控制的,请进行比较以找出谁在签入更改。

标签: c# visual-studio asp.net-core visual-studio-2017


【解决方案1】:

端口可能正在更改,因为源代码管理忽略了launchSettings.json。例如This common gitignore file,排除:

**/Properties/launchSettings.json

Visual Studio 2017 将 ASP.NET Core 服务器设置(适用于 IIS Express 和 Kestrel)存储在此文件中。如果它被源代码控制忽略,它将在每台机器上使用随机端口重新生成。如果您签入文件,每台机器都将使用相同的服务器设置。

【讨论】:

  • 这正是正在发生的事情。谢谢!
【解决方案2】:

您可以为此使用 UseUrls:

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

        host.Run();
    }
}

UPD:

传递参数的替代方法:

dotnet run --urls http://0.0.0.0:5000

【讨论】:

    【解决方案3】:

    更改端口绑定

    .vs/config/applicationhost.config
    

    在节点上

    configuration/system.applicationHost/sites/site[<name>]/bindings/binding[bindingInformation]
    

    来自类似的值

    <binding protocol="http" bindingInformation="*:5000:*" />
    

    到这样的值

    <binding protocol="http" bindingInformation=":5000:" />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-06
      • 2019-08-24
      • 1970-01-01
      • 1970-01-01
      • 2019-04-22
      • 1970-01-01
      • 1970-01-01
      • 2018-05-13
      相关资源
      最近更新 更多