ASP.NET核心获取URL绑定 [英] ASP.NET core get URL bindings

查看:328
本文介绍了ASP.NET核心获取URL绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

启动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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆