通过 azure VM 访问 .net Core API 抛出 ERR_CONNECTION_REFUSED

Accessing .net Core API via azure VM throws ERR_CONNECTION_REFUSED

我已经使用 Azure 设置了一个 linux VM,并且我从该机器获得了 public IP。

我已经在那台机器中克隆了我的存储库,我 运行 dotnet run 得到了输出:

Hosting environment: Development
Content root path: /home/g67Admin/projeto_integrador_grupo67/mdv
Now listening on: https://localhost:5001
Now listening on: http://localhost:5000
Application started. Press Ctrl+C to shut down.

然后我尝试去:https://(public IP):5001/api/Drivers 但我无法访问它,给我这个错误 ERR_CONNECTION_REFUSED

我已经访问了 Azure 门户并尝试授权端口 5000 和 5001 从任何源到任何源,多次重启机器,但仍然连接被拒绝。

我该如何解决这个问题?

默认情况下,您的本地开发服务器不会侦听您的 public IP 或允许来自您网络中任何其他机器的流量。它只监听 localhost。您需要将其他网址显式绑定到您的服务器。

https://weblog.west-wind.com/posts/2016/sep/28/external-network-access-to-kestrel-and-iis-express-in-aspnet-core

您可以 运行 您的代码如下:

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

或者您可以使用 Use WebHost.UseUrls() 来获得类似的结果。