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

3

13

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?

Chavannes answered 17/7, 2017 at 14:45 Comment(1)
I have also turned off my firewall, and do not have any 3rd party anti-viruses running...Chavannes
M
12

IIS Express does not allow remote connections by default. This post explains how to activate it.

In my opinion, you would be better off if you setup local IIS and publish the app to the configured IIS folder.

I already tried HTTP.SYS configurations for other situations, and sometimes the configs are lost whether on restarts or windows updates... But this may not happen to you...

Milissa answered 17/7, 2017 at 16:11 Comment(3)
Thanks, the free VS extension from visualstudiogallery.msdn.microsoft.com/ worked wondersChavannes
Here's the direct link to the extension marketplace.visualstudio.com/…Eburnation
Extension doesn't do anything, waste of timeHominoid
H
3

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://learn.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://learn.microsoft.com/en-us/aspnet/core/fundamentals/servers/kestrel/host-filtering?view=aspnetcore-5.0

Horowitz answered 9/9, 2021 at 11:10 Comment(0)
D
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]:*" />
Dagostino answered 13/4, 2021 at 19:55 Comment(1)
After this change my .net framework app wasn't reachable anymore at all. Not through localhost, 127.0.0.1, or my localhost alias. Chrome just says ERR_CONNECTION_REFUSED.Garibold

© 2022 - 2024 — McMap. All rights reserved.