var host = new WebHostBuilder() .UseKestrel() .UseStartup() .Build(); var localUrl = host.Services.GetService ().GetLocalApiUrl();
public Startup(IConfiguration configuration, IServerApplicationHost serverApplicationHost) { Configuration = configuration; _serverApplicationHost = serverApplicationHost; } public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { // ... var localUrl = _serverApplicationHost.GetLocalApiUrl(); // ... }This code is taken from the Startup class of an ASP.NET Core application. It receives an instance of IServerApplicationHost via dependency injection in the constructor, and gets the local API URL using the GetLocalApiUrl method in the Configure method. This URL can then be used to configure the application's services or third-party components.