In ASP.NET Core 10, which address does Kestrel listen on when nothing configures a URL, and how do you make it accept connections from other machines?
answer
- nothing set: a built-in fallback
- loopback only, plain HTTP
- launchSettings applicationUrl becomes ASPNETCORE_URLS
- ListenAnyIP vs ListenLocalhost
- or * means every interface
basics
~10 sWith no URL, port or endpoint configured, Kestrel binds http://localhost:5000: plain HTTP on the IPv4 and IPv6 loopback addresses only. To accept other machines, bind every interface, for example ASPNETCORE_URLS=http://+:5000, HTTP_PORTS=5000 or ListenAnyIP(5000).
solid answer
~40 sWhen nothing supplies an address, Kestrel falls back to `http://localhost:5000` and logs `Now listening on: http://localhost:5000`. That is HTTP only and loopback only, so a request from another host, or from outside a container, never connects. Under `dotnet run` you rarely see it, because the SDK copies the launch profile's `applicationUrl` from `launchSettings.json` into `ASPNETCORE_URLS`, and the templates write a random port there. To listen on every interface, use a wildcard host: `ASPNETCORE_URLS=http://+:5000` (or `*`), the `--urls` argument, `ASPNETCORE_HTTP_PORTS=5000`, or `options.ListenAnyIP(5000)` inside `builder.WebHost.ConfigureKestrel`. `localhost` binds both loopback addresses, `0.0.0.0` binds IPv4 only, and a host name such as `api.example.com` binds all interfaces, because Kestrel does not filter by host name.
code
csharp · 11 linesvar builder = WebApplication.CreateBuilder(args);
builder.WebHost.ConfigureKestrel(options =>
{
// Every IPv4 and IPv6 interface on port 5000
options.ListenAnyIP(5000);
});
var app = builder.Build();
app.MapGet("/health/ping", () => "ok");
app.Run();go deeper
Recall http://localhost:5000 as the fallback, why loopback is unreachable from outside, and one way to bind every interface such as ASPNETCORE_URLS=http://+:5000.
Explain how dotnet run injects ASPNETCORE_URLS from launchSettings.json, and how Kestrel reads localhost, IP literals, wildcards and host names in a URL.
Show you would make the binding explicit per environment, pick one mechanism, and recognise the overriding-address warning when code and settings disagree.
Discuss which team owns listening addresses across environments, and why explicit configuration beats relying on a server fallback that differs between dotnet run and production.
## What Kestrel is and why the default matters **Kestrel** is the cross-platform HTTP server that ships inside ASP.NET Core. `WebApplication.CreateBuilder` registers it, so a plain `app.Run()` starts Kestrel in your own process. Kestrel needs at least one **endpoint**, an address and port to listen on, and it collects them from several places: code in `ConfigureKestrel`, the `Kestrel:Endpoints` configuration section, the `urls` setting (`ASPNETCORE_URLS`, `--urls`), and the `HTTP_PORTS` / `HTTPS_PORTS` settings. When **none** of these supplies anything, Kestrel uses a constant built into the server, `http://localhost:5000`. The hosting layer then logs `Now listening on: http://localhost:5000` at Information level. ## What `http://localhost:5000` really binds The fallback is narrow on purpose: - **HTTP only.** No HTTPS endpoint is created without configuration. - **Loopback only.** `localhost` is not resolved through DNS. Kestrel binds the IPv4 loopback `127.0.0.1` and the IPv6 loopback `[::1]` separately, and if IPv6 is unavailable it logs that binding and keeps the IPv4 one. - **Port 5000.** The port is fixed in the fallback, not chosen at random. A loopback socket accepts connections from the same machine and nothing else. That is why the classic first-deployment symptom is a service that answers `curl http://localhost:5000` on the box but times out from every other machine, or from the host when the app runs in a container. ## Why `dotnet run` shows a different port The project templates write `Properties/launchSettings.json` with an `http` and an `https` profile whose `applicationUrl` the template engine fills with a generated port (HTTP in the 5000-5300 range, HTTPS in 7000-7300). `dotnet run` reads the profile and sets **`ASPNETCORE_URLS`** to that `applicationUrl` before starting the app. So: 1. `dotnet run` binds the profile's random port, for example `http://localhost:5123`. 2. `dotnet run --no-launch-profile` skips the profile. 3. `dotnet MyApp.dll`, a published app, or a service manager starting the binary never reads `launchSettings.json`, so with nothing else configured it falls back to `http://localhost:5000`. `launchSettings.json` is a development-time file; it is not part of the published output's runtime behaviour. ## Making Kestrel reachable from other machines You bind a wildcard or a specific non-loopback address. Any of these works: | Mechanism | Example | What it binds | |---|---|---| | `urls` setting | `ASPNETCORE_URLS=http://+:5000` | every interface, port 5000 | | command line | `dotnet MyApp.dll --urls http://*:5000` | every interface | | `HTTP_PORTS` (since .NET 8) | `ASPNETCORE_HTTP_PORTS=5000` | expands to `http://*:5000` | | code | `options.ListenAnyIP(5000)` | IPv6 any (dual-mode), falling back to IPv4 any | | code | `options.Listen(IPAddress.Any, 5000)` | IPv4 interfaces only | | code | `options.ListenLocalhost(5000)` | loopback only, like the default | The host part of a URL is interpreted like this: - `localhost` (and, in ASP.NET Core 10, any `*.localhost` name) means the loopback addresses. - An IP literal such as `0.0.0.0` or `10.0.0.12` binds exactly that address; `0.0.0.0` is IPv4 only. - `+`, `*` or **any other host name** binds every interface. Kestrel does not resolve `api.example.com` and bind its IP; it treats the name like a wildcard. Restricting which `Host` headers are accepted is a different job (host filtering, `AllowedHosts`). A URL must not carry a path: `http://+:5000/api` fails at startup with a message telling you to call `UsePathBase()` instead. ## Code version ```csharp var builder = WebApplication.CreateBuilder(args); builder.WebHost.ConfigureKestrel(options => { options.ListenAnyIP(5000); // all interfaces, IPv4 and IPv6 }); var app = builder.Build(); app.MapGet("/health/ping", () => "ok"); app.Run(); ``` Endpoints declared in code like this take precedence over `ASPNETCORE_URLS` and `HTTP_PORTS`; Kestrel logs a warning that it is overriding those addresses. Choose one mechanism per deployment so the effective binding is obvious. ## Checking what actually bound When a deployment is unreachable, confirm the binding before suspecting the network: 1. Read the `Now listening on:` lines at startup. `http://localhost:5000` or `http://127.0.0.1:...` means loopback only. 2. Check the environment the process really received (`ASPNETCORE_URLS`, `ASPNETCORE_HTTP_PORTS`), not the one in your shell or IDE. 3. On the host, list listening sockets (`ss -ltn` on Linux, `netstat -an` on Windows) and see whether the port is bound to `127.0.0.1` or to all addresses. 4. If code calls `ListenLocalhost` or `Listen(IPAddress.Loopback, ...)`, environment settings will not widen it, because endpoints in code win over URL settings. ## Interview angle A junior answer names `http://localhost:5000` and explains why it is unreachable from outside. A stronger one adds that `dotnet run` hides the default by setting `ASPNETCORE_URLS` from the launch profile, and that `+`, `*` and host names all mean "every interface".
- Why does the app listen on a port like 5123 under dotnet run but on 5000 when you start the DLL directly?`dotnet run` applies the launch profile from `Properties/launchSettings.json` and sets `ASPNETCORE_URLS` to its `applicationUrl`, which the template filled with a generated port. Starting the DLL never reads that file, so with nothing else configured Kestrel falls back to `http://localhost:5000`. `dotnet run --no-launch-profile` reproduces the production behaviour locally.
- What is the difference between ListenAnyIP(5000) and Listen(IPAddress.Any, 5000)?`ListenAnyIP` first binds the IPv6 any address, which on dual-mode sockets also accepts IPv4, and falls back to `IPAddress.Any` if IPv6 is unavailable. `Listen(IPAddress.Any, 5000)` binds the IPv4 any address only, so IPv6 clients cannot connect.
- Does ASPNETCORE_URLS=http://api.example.com:5000 restrict Kestrel to that host name?No. A host that is neither `localhost` nor an IP literal is treated like a wildcard, so Kestrel binds every interface on port 5000 and accepts any `Host` header. To reject other host names you configure host filtering (`AllowedHosts`), which is a separate middleware concern.
- Kestrel listens on all interfaces on port 5000 by default.
- The default also opens https://localhost:5001 automatically.
- launchSettings.json applies when the published app starts in production.
- http://api.example.com:5000 binds only the IP that name resolves to.
- ListenLocalhost(5000) makes the app reachable from other machines.
- 0.0.0.0 in the URL also covers IPv6 clients.