Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET core get URL bindings

Tags:

asp.net-core

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.

How can I do this?

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.

like image 646
ZoolWay Avatar asked Oct 17 '25 18:10

ZoolWay


1 Answers

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

Console.WriteLine(string.Join(", ", addresses)); // will write "http://localhost:5000"

Thanks to source of WebHost class

like image 121
Dmitry Avatar answered Oct 20 '25 17:10

Dmitry