asp.net-core – 无法从命令行更改Kestrel侦听端口

我有以下切入点:

public static void Main(string[] args)
{
    var config = new ConfigurationBuilder()
        .SetBasePath(Directory.GetCurrentDirectory())
        .AddJsonFile("hosting.json", optional: true)
        .AddCommandLine(args)
        .AddEnvironmentVariables()
        .Build();

    var host = new WebHostBuilder()
        .UseConfiguration(config)
        .UseKestrel()
        .UseContentRoot(Directory.GetCurrentDirectory())
        .UseIISIntegration()
        .UseStartup<Startup>()
        .Build();

    host.Run();
}

如果我添加hosting.json文件就可以了

{
  "server.urls": "http://0.0.0.0:5001"
}

或者如果我定义环境变量(找到名称here)

SET ASPNETCORE_URLS=https://0.0.0.0:5001

但是如果我将–server.urls http://0.0.0.0:5001作为参数传递,那么app会监听默认的5000端口:

> dotnet run --server.urls http://0.0.0.0:5001
...
Now listening on: http://localhost:5000

最佳答案 正确的语法是

dotnet run --server.urls=http://0.0.0.0:5001

代替

dotnet run --server.urls http://0.0.0.0:5001

有关详细信息,请参见the old answer.

点赞