ASP.NET core webhost 启动后,我想获取它的绑定 URL(即“http://0.0.0.0:5001 http://0.0.0.0:5001", "https://192.168.42.42:8081 https://192.168.42.42:8081”等)。所以在处理所有配置内容后它绑定到的 URL。
我怎样才能做到这一点?
注意:我不处理请求。服务器应该只是记录它或将信息发送到其他地方。我找到了很多关于如何设置的信息URLs但我想询问主机它绑定了什么,而不是询问配置它应该绑定什么。
var server = app.ApplicationServices.GetRequiredService<IServer>();
var addresses = server.Features?.Get<IServerAddressesFeature>()?.Addresses;
Console.WriteLine(string.Join(", ", addresses)); // will write "http://localhost:5000"
感谢来源网络主机类 https://github.com/aspnet/Hosting/blob/80ae7f056c08b740820ee42a7df9eae34541e49e/src/Microsoft.AspNetCore.Hosting/Internal/WebHost.cs#L228-L230
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)