ASP.NET核心获取URL绑定 [英] ASP.NET core get URL bindings
问题描述
启动ASP.NET核心Web主机后,我想获取其绑定URL(即" http://0.0.0.0:5001 "," https://192.168.42.42:8081 ",等等.).因此,在处理完所有配置内容后,它绑定到的URL.
After a ASP.NET core webhost has started I would like to get its binding URLs (i.e. "http://0.0.0.0:5001", "https://192.168.42.42:8081", etc.). So the URLs it has bound to after processing all that configuration stuff.
我该怎么做?
注意:我没有处理请求.服务器应该将其记录下来或将信息发送到其他地方.我发现了很多有关如何设置 URL 的信息,但我想问一下主机绑定的内容,而不是询问配置应该绑定的内容.
Note: I am not processing a request. The server should just log it or send the information elsewhere. I find lots of information on how to set URLs but I would like to ask the host what it has bound to instead of asking the configuration what it should have bound to.
推荐答案
var server = app.ApplicationServices.GetRequiredService<IServer>();
var addresses = server.Features?.Get<IServerAddressesFeature>()?.Addresses;
Console.WriteLine(string.Join(", ", addresses)); // will write "http://localhost:5000"
感谢 WebHost类
这篇关于ASP.NET核心获取URL绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!