Reputation: 5505
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.
Upvotes: 4
Views: 1558
Reputation: 16825
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
Upvotes: 6