ASP.NET core 获取 URL 绑定

Zoo*_*Way 4 asp.net-core

ASP.NET core webhost 启动后,我想获取其绑定 URL(即“ http://0.0.0.0:5001 ”、“ https://192.168.42.42:8081 ”等)。所以在处理完所有配置内容后它绑定到的 URL。

我怎样才能做到这一点?

注意:我不处理请求。服务器应该只是记录它或将信息发送到其他地方。我找到了很多关于如何设置URL 的信息,但我想询问主机它绑定了什么,而不是询问配置它应该绑定什么。

Dmi*_*try 6

var server = app.ApplicationServices.GetRequiredService<IServer>();
var addresses = server.Features?.Get<IServerAddressesFeature>()?.Addresses;

Console.WriteLine(string.Join(", ", addresses)); // will write "http://localhost:5000"
Run Code Online (Sandbox Code Playgroud)

感谢WebHost 类的来源