Bad Request - Invalid Hostname when accessing localhost Web API or Web App from across LAN

Viewed 30124

I have an ASP .Net Core 1.1 Web API and Web App running on my localhost from Visual Studio 2017 on Windows 10.

When I run the projects, the API runs on http://localhost:50082/api/ and the web app on http://localhost:60856/

However, if others on the LAN try to access it (using my computer's IP address - http://192.168.1.101:60856/ they get a

Bad Request - Invalid Hostname

Error. In fact,. I get this error too of I use my IP address. I've tried about a dozen variations in my C:\Users\Documents\IISExpress\config\applicationhost.config file, such as:

<bindings>
<binding protocol="http" bindingInformation="*:60856:localhost" />
</bindings>

and

<bindings>
<binding protocol="http" bindingInformation="*:60856:" />
</bindings>

restarting the projects (and therefore IIS Express) every time, but nothing seems to work. Any ideas?

3 Answers

If you are using asp core, another solution is to simply change your profile from 'IIS' to 'Standalone' (which is named as your project) from Visual Studio toolbar:

enter image description here

By this change, IIS will not run anymore and the embedded asp web server 'kestrel' will face the requests directly.

If it's still give you host invalid error adjust your hostname settings by adding this line to your config file:

"AllowedHosts": "*",

More info:

https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/?view=aspnetcore-5.0#set-up-a-reverse-proxy

https://weblog.west-wind.com/posts/2019/Mar/16/ASPNET-Core-Hosting-on-IIS-with-ASPNET-Core-22

https://docs.microsoft.com/en-us/aspnet/core/fundamentals/servers/kestrel/host-filtering?view=aspnetcore-5.0

I had this same issue trying to get ngrok to point to a local ASP.NET Core website.

This guy's answer worked for me: https://kodlogs.com/blog/532/iis-express-http-error-400-the-request-hostname-is-invalid

Basically, in the [solution dir]\.vs\[asp.net core web app dir]\config\applicationhost.config file, change the line from:

<binding protocol="https" bindingInformation="*:[your port]:localhost" />

to:

<binding protocol="https" bindingInformation="*:[your port]:*" />
Related