Bad Request - Invalid Hostname with HTTP Error 400. The request hostname is invalid.
from a Kestrel app means the Host header of the request is not in
AllowedHosts in appsettings.json. Add the host name the requests
really arrive with, separated by semicolons, or call the app by a name that is already in
the list.
In this test AllowedHosts held only api.example.test. Requests to
127.0.0.1 and to localhost were refused; the same request with
Host: api.example.test got through. Adding localhost to the list
fixed the local calls.
The error
This is the HTTP response body; the server console printed nothing about it at the template's default log levels:
PS> curl.exe -s -i http://127.0.0.1:15630/
HTTP/1.1 400 Bad Request
Content-Length: 334
Content-Type: text/html
Date: Sat, 14 Nov 2026 01:17:27 GMT
Server: Kestrel
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">
<HTML><HEAD><TITLE>Bad Request</TITLE>
<META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii"></ HEAD >
<BODY><h2>Bad Request - Invalid Hostname</h2>
<hr><p>HTTP Error 400. The request hostname is invalid.</p>
</BODY></HTML>
Why it happens
The settings file had this line (the rest was the template's logging section):
...
"AllowedHosts": "api.example.test"
...
The app compares the Host header of each request with that list and answers
400 when it does not match. A rerun with curl.exe -v, which prints the
request it sends, showed the Host line of each request; both got the 400
page:
PS> curl.exe -s -v http://127.0.0.1:15630/
...
> GET / HTTP/1.1
> Host: 127.0.0.1:15630
...
< HTTP/1.1 400 Bad Request
...
PS> curl.exe -s -v http://localhost:15630/
...
> GET / HTTP/1.1
> Host: localhost:15630
...
< HTTP/1.1 400 Bad Request
...
Forcing the header proved the filter was the cause, not the address:
PS> curl.exe -s -i -H "Host: api.example.test" http://127.0.0.1:15630/
HTTP/1.1 200 OK
Content-Type: text/plain; charset=utf-8
Date: Sat, 14 Nov 2026 01:17:27 GMT
Server: Kestrel
Transfer-Encoding: chunked
Clinic API is running
The page looks like a generic web server error, but the Server: Kestrel
header shows it came from the app itself, so the place to look is the app's own settings
file.
The fix
...
"AllowedHosts": "api.example.test;localhost"
...
PS> curl.exe -s -i http://localhost:15630/
HTTP/1.1 200 OK
Content-Type: text/plain; charset=utf-8
Date: Sat, 14 Nov 2026 01:17:29 GMT
Server: Kestrel
Transfer-Encoding: chunked
Clinic API is running
The entry localhost matched a request whose header was
localhost:15630, so the port is not part of the entry. The template's value
is "*"; if you keep a list instead, it has to hold every name the app is
reached by, including the one you use on your own machine. The app was restarted after
the file was edited.
How it was reproduced
A minimal API made with dotnet new web -f net10.0 on SDK 11.0.100, running on
Microsoft.AspNetCore.App 10.0.15 in the Production environment, built and started by a small script, start.ps1, which runs dotnet build -nologo -v q in the project folder and then bin\Debug\net10.0\FixLab.exe --urls http://127.0.0.1:15630, with AllowedHosts
changed in the template's appsettings.json. Requests were sent with
curl.exe -s -i to 127.0.0.1, to localhost and with
a forced Host header; api.example.test is a made-up name.
Frequently asked
- What causes '400 Bad Request - Invalid Hostname' in ASP.NET Core?
- The Host header of the request is not in AllowedHosts in appsettings.json. In the test, AllowedHosts held only api.example.test, so requests to 127.0.0.1 and localhost got the 400 page.
- How do I allow several hosts in AllowedHosts?
- Separate them with semicolons, for example "api.example.test;localhost". In the test that value let both localhost:15630 and a request with Host: api.example.test through.
- Does AllowedHosts need the port number?
- No, not in the test. The entry localhost matched a request whose Host header was localhost:15630.
More decoded errors in the Fixes category.