[iis-7] Bad Request - Invalid Hostname IIS7

When I try to hit my web app on port 8080 I get the following error

Bad Request - Invalid Hostname
HTTP Error 400. The request hostname is invalid.

I don't even know where to begin to diagnose this problem

This question is related to iis-7

The answer is


You can use Visual Studio 2005/2008/2010 CMD tool. Run it as admin, and write

aspnet_regiis -i

At last I can run my app successfully.


I saw the same error after using msdeploy to copy the application to a new server. It turned out that the bindings were still using the IP address from the previous server. So, double check IP address in the IIS bindings. (Seems obvious after the fact, but did not immediately occur to me to check it).


For Visual Studio 2017 and Visual Studio 2015, IIS Express settings is stored in the hidden .vs directory and the path is something like this .vs\config\applicationhost.config, add binding like below will work

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

Syntax: https://docs.microsoft.com/en-us/dotnet/api/microsoft.web.administration.binding.bindinginformation?view=iis-dotnet


So, I solved this by going to my website in IIS Manager and changing the host name in site bindings from localhost to *. Started working immediately.

Site Bindings in IIS


FWIW, if you'd like to just allow requests directed to any hostname/ip then you can set your binding like so:

<binding protocol="http" bindingInformation="*:80:*" />

I use this binding so that I can load a VM with IE6 and then debug my application.


EDIT: While using IIS Express to debug, the default location for this option's config file is

C:\Users\{User}\Documents\IISExpress\config\applicationhost.config

I got this error when I tried to call a webservice using "localhost". I fixed it by using the actual IP instead (192.168...)


Check your local hosts file (C:\Windows\System32\drivers\etc\hosts for example). In my case I had previously used this to point a URL to a dev box and then forgotten about it. When I then reused the same URL I kept getting Bad Request (Invalid Hostname) because the traffic was going to the wrong server.


Make sure IIS is listening to your port.

In my case this was the issue. So I had to change my port to something else like 8083 and it solved this issue.


This page by Microsoft describes how to set up access to IIS Server Express from other computers on the local network.

In a nutshell:

1) from a command prompt with admin privileges:

netsh http add urlacl url=http://[your ip address]:8181/ user=everyone

2) In Windows Firewall with Advanced Security, create a new inbound rule for port 8181 to allow external connections

3) In applicationhost.config, in the node for your project, add:

<binding protocol="http" bindingInformation="*:8181:[your ip address]" />

Do NOT add (as was suggested in another answer):

<binding protocol="http" bindingInformation="*:8181:*" />

The above wildcard binding broke my access from http://192.168.1.6:8181/


This solved my problem (sorry for my bad English):

  1. open cmd as administrator and run command (Without the square brackets):
    netsh http add urlacl url=http://[ip adress]:[port]/ user=everyone

  2. in documents/iisexpress/config/applicationhost.config and in your root project folder in (hidden) folder: .vs/config/applicationhost.config you need add row to "site" tag:
    <binding protocol="http" bindingInformation="*:8080:192.xxx.xxx.xxx" />

  3. open "internet information services (iis) manager"
    (to find it: in search in taskbar write "Turn Window features on or off" and open result and then check the checkbox "internet information service" and install that):

    1. in left screen click: computer-name --> Sites --> Default Web Site and
    2. then click in right screen "Binding"
    3. click Add button
    4. write what you need and press "OK".
  4. open "Windows Firewall With Advanced Security",

    1. in left screen press "Inbound Rules" and then
    2. press in right screen "New Rule..."
    3. check port and press Next,
    4. check TCP and your port and press Next,
    5. check "Allow the connection" and press Next,
    6. check all checkbox and press Next,
    7. write name and press Finish.
  5. done.


If working on local server or you haven't got domain name, delete "Host Name:" field. enter image description here


Double check the exact URL you're providing. I saw this error when I missed off the route prefix defined in ASP.NET so it didn't know where to route the request.


Don't forget to bind to the IPv6 address as well! I was trying to add a site on 127.0.0.1 using localhost and got the bad request/invalid hostname error. When I pinged localhost it resolved to ::1 since IPv6 was enabled so I just had to add the additional binding to fix the issue.

IIS Site Bindings


I'm not sure if this was your problem but for anyone that's trying to access his web application from his machine and having this problem:

Make sure you're connecting to 127.0.0.1 (a.k.a localhost) and not to your external IP address.

Your URL should be something like http://localhost:8181/ or http://127.0.0.1:8181 and not http://YourExternalIPaddress:8181/.


Additional information:
The reason this works is because your firewall may block your own request. It can be a firewall on your OS and it can be (the usual) your router.

When you connect to your external IP address, you connect to you from the internet, as if you were a stranger (or a hacker).
However when you connect to your localhost, you connect locally as yourself and the block is obviously not needed (& avoided altogether).