This is not exactly a common scenario, but here it is just the same.

From within a ASP.NET WebAPI end point, I need to know the IP addresses and the ports that the current web application is being served on.

There may be more than one, because the server might be supporting both HTTP and HTTPS traffic.

ServerApplication

Luckily, this information is available via injecting the an instance of IServer into your endpoint:

app.MapGet("/", (IServer server, ILogger<Program> logger) =>
{
  //
  // Your logic in here
  // 
});

app.Run();

From this, we can query the Features property for what we are interested in, in this case, a collection of IServerAddressesFeature

app.MapGet("/", (IServer server, ILogger<Program> logger) =>
{
  // Query the server addresses
  var addresses = server.Features.Get<IServerAddressesFeature>()?.Addresses;
  // Log the number found
  logger.LogInformation("Listening on {AddressCount} addresses", addresses?.Count ?? 0);
  // Loop through the results and log
  foreach (var address in addresses!)
      logger.LogInformation("- {Address}", address);
});

This will print the following to your logs:

info: Program[0]
      Listening on 2 addresses
info: Program[0]
      - https://localhost:7065
info: Program[0]
      - http://localhost:5195

ServerPortsAPI

With the URL and ports, you can then proceed to use them as necessary.

TLDR

You can obtain the URL and ports of the running web server from an API endpoint by injecting the IServer interface.

The code is in my GitHub.

Happy hacking!