C# Web - localhost:port works, 127.0.0.1:port doesn't work C# Web - localhost:port works, 127.0.0.1:port doesn't work android android

C# Web - localhost:port works, 127.0.0.1:port doesn't work


So typically an HTTP 400 Invalid Hostname error usually means you do not have the website set accept all hostnames and/or IP addresses. I presume because this is a C# application you are hosting this on IIS. To fix this open up IIS Manager (Win+R and enter inetmgr), expand the server then Sites, then right-click on the the website where your application is hosted and select bindings. In this list there should be an http binding for port 54408, double-click on this. Under IP address make sure All Unassigned is selected, and under Host name make sure the field is blank. Hit OK, then Close. The setting should take effect immediately without needing to reset IIS.

If you are only testing this through Visual Studio's built in web deployment, there are similar settings to the above somewhere in VS (I'm a little rusty on it so I can't remember exactly where or how). Alternatively you can set up a web site in IIS exactly how you want it and then have VS deploy to that website instead of using its own internal server. I think there might be some debugging drawbacks to doing it this way though (once again I'm a little fuzzy on details, I'll edit them in when I remember them or figure it out).

A little background as to why these settings exist:Sometimes a server needs to host multiple sites that are all to be accessed via port 80. So lets say we have foo.com and bar.com and they are too small to warrant getting a separate server for both of them. So instead they are both hosted on a sever with an IP address of 1.2.3.4. Now when you enter the URL foo.com into the browser and hit go, it first resolves the host name to 1.2.3.4, and then it creates a request and part of that request is called the host header. The host header is filled with the hostname of the URL entered, in this case foo.com. When the request is received by the server, it looks at the host header and serves back the content for foo.com.

Now if you were to try and enter 1.2.3.4 into the browser, it would create a request with a blank host header, because none was specified. When the request is received by the server, it doesn't know what to do because there are two websites being hosted by the server and there is no host header to specify which one the browser is looking for, so instead it returns an error.

This is probably what is happening in your situation. Your website is being hosted under the localhost hostname and any other request isn't being responded to. The settings I specified to change are basically telling the server that no matter what IP address (network interface) it comes in on, and no matter what the hostname it is looking for, as long as it is coming in on port 54408, serve this website.


If you are using Visual Studio's built in web server (IIS Express), localhost is mapped by default; to enable 127.0.0.1:

1) At path: %USERPROFILE%\Documents\IISExpress\config

2) Locate config file: applicationhost.config

3) - open config file in editor (I use notepad++)

4) Search for the site port, for example if the url is typically localhost:57578, search "57578" and you should find:

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

5) Change this entry to:

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

6) Save and exit, restart website.

Note: You will want to repeat this process any time you create a new virtual directory (changing the port number Project/Properties/Web/Project Url), which creates a new entry in the applicationhost.config file.


In visual studio 2015 with IIS express:first in project properties change project url from http://localhost:(Port No) to : http://127.0.0.1:(Port No)

and click on Create Visual Directory

then go to your solution path and open ".vs" dirctory (directory is hidden) and open "applicationhost.config"

<sites>        <site name="WebSite1" id="1" serverAutoStart="true">            <application path="/">                <virtualDirectory path="/" physicalPath="%IIS_SITES_HOME%\WebSite1" />            </application>            <bindings>                <binding protocol="http" bindingInformation=":8080:localhost" />            </bindings>        </site>        <site name="####" id="2">            <application path="/" applicationPool="Clr4IntegratedAppPool">                <virtualDirectory path="/" physicalPath="E:\Visual Studio 2015\Projects\####\####" />            </application>            <bindings>                <binding protocol="http" bindingInformation="*:5050:localhost" />            </bindings>        </site>        <site name="####(1)" id="3">            <application path="/" applicationPool="Clr4IntegratedAppPool">                <virtualDirectory path="/" physicalPath="E:\Visual Studio 2015\Projects\####\####" />            </application>            <bindings>                <binding protocol="http" bindingInformation="*:5050:127.0.0.1" />            </bindings>        </site>        <siteDefaults>            <logFile logFormat="W3C" directory="%IIS_USER_HOME%\Logs" />            <traceFailedRequestsLogging directory="%IIS_USER_HOME%\TraceLogFiles" enabled="true" maxLogFileSizeKB="1024" />        </siteDefaults>        <applicationDefaults applicationPool="Clr4IntegratedAppPool" />        <virtualDirectoryDefaults allowSubDirConfig="true" />    </sites>

and change last binding section of your web app from *:(Port No):localhost to *:(Port No):127.0.0.1

and stop all running project on IIS Express and restart them.