'Bad Request - Invalid Hostname with Asp.net WebAPI project in Visual Studio 2013
I am running a very basic webAPI project in Visual Studio Pro 2013. It runs fine on localhost on my machine. I then try and go to a browser from a different machine and goto :57571 similiar to how i could point to rails apps by putting the servers ipaddress followed by the port number. I then get Bad Request - Invalid Hostname HTTP Error 400. The request hostname is invalid.
I also replicated this by putting two windows machines in azure. One running visual studio pro 2013. I run the same application and it works fine locally. Then if i try and point at it from another VM (on the same virtual network, i can also ping the server vm) I still get Bad Request - Invalid Hostname HTTP Error 400. The request hostname is invalid.
Mind you I dont care about a custom domian I just want to be able to run this app from another machine using the IP address. Is this possible? (if so any ideas on what i am doing wrong?) Here is what i tried Didnt work for me This also didnt make it work
How can i tell visual studio is running as an admin?
(I am using windows Server 2012 R2 where visual studio is running. I turned off the firewall on both machines)
Solution 1:[1]
Here is how I got it to work.
goto C:\users\yourusername\Documents\IISExpress\config\applicationhost.config in Visual Studio 2013
Add the following line to applicationhost.config made it to work
<binding protocol="http" bindingInformation="*:53676:*" />
where this didn't work
<binding protocol="http" bindingInformation="<clientsIPaddress>:53676:*" />
Vs2015 Update and the exact location for this change the application.config are outlined below.
The path to the file is the following for VS 2015.
C:\Users\\{YourUsername}\Documents\Visual Studio 2015\Projects\\{ThisSolutionName}\\.vs\config\applicationhost.config
as Daniel mentioned.
Where do I put this in my application.config?
Below are the parent sections in the XML file where you would find the bindings.
<system.applicationHost>
<sites>
<site name="WebSite1" id="1" serverAutoStart="true">
<bindings>
<binding protocol="http" bindingInformation=":8080:localhost" />
</bindings>
</site>
</sites>
</system.applicationHost>
This was roughly line 161 for a new project that didn't change anything in the applicationhost.config.
What I didn't realise is what I thought the clients IP address was, actually wasn't true. The reason being is that I had a VNet to VNet connection in Azure and the gateway that is connecting the two IP addresses reassigns the clients IP address on the network in which my application was running. I thus had to look up the new IP address it was mapped to in order to not use the *:port:*
strategy
Solution 2:[2]
I can not comment, but wanted to answer @EthanKeiser
Visual Studio 2015 has a separate config file for each Solution that you will need to update per the accepted answer. This file is located at:
C:\Users\\{UserName}\Documents\Visual Studio 2015\Projects\\{SolutionName}\\.vs\config\applicationhost.config
Solution 3:[3]
Had the same issue for an hour, tried everything here and related to this problem, but what fixed it was running Visual Studio as Administrator. Otherwise I'd keep getting Unable to connect to web server 'IIS Express'
error which leads you in the wrong way.
Solution 4:[4]
Another thing that helped me is to add 127.0.0.1
to the binding.
So the result applicationhost.config
looks like this:
<bindings>
<binding protocol="http" bindingInformation="*:28066:" />
<binding protocol="http" bindingInformation="*:28066:localhost" />
<binding protocol="http" bindingInformation="*:28066:127.0.0.1" />
<binding protocol="http" bindingInformation="*:28066:sitename.com" />
<binding protocol="https" bindingInformation="*:44302:" />
<binding protocol="https" bindingInformation="*:44302:localhost" />
<binding protocol="https" bindingInformation="*:44302:127.0.0.1" />
<binding protocol="https" bindingInformation="*:44302:sitename.com" />
</bindings>
And hosts
file is like this:
127.0.0.1 localhost
127.0.0.1 sitename.com
Before adding 127.0.0.1
it was OK in the browser to navigate like https://localhost:44302
, but https://sitename.com:44302
resulted in Bad Request
.
Also, if you do the 'hot-replacement', make sure you're reseting IIS. You can do that by right-clicking on the project in the Visual Studio, selecting Unload project
, then change applicationhost.config
, save it, and Reload project
in VS.
By the way, don't forget to generate SSL certificate for your project. I'm not sure that Visual Studio will do that for you.
Port should be in range from 44300 to 44399.
Here's how you can generate SSL:
- Open Visual Studio Developers Tools as Administrator
makecert -r -pe -n CN="dev.helpme.transwestern.com" -eku 1.3.6.1.5.5.7.3.1 -ss my -sr localmachine -sky exchange -sp "Microsoft RSA SChannel Cryptographic Provider" -sy 12
netsh http show sslcert ipport=0.0.0.0:44302
(you will need to get appid from this step)netsh http delete sslcert ipport=0.0.0.0:44302
Now, open "Manage computer certificates", navigate to the Personal and select newly created. You will need "Details/Thumbprint".
netsh http add sslcert ipport=0.0.0.0:44302 certhash=<thumbprint> appid={id from step 3}
(include bracets {})
Hope that helps.
Solution 5:[5]
For VS2019 and 2022, you might find the applicationhost.config in this folder instead. The format seems to be the same as the above answers. ${projectRoot}/.vs/config/applicationhost.config
.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Dean Meehan |
Solution 2 | James A Mohler |
Solution 3 | user1306322 |
Solution 4 | Vlad |
Solution 5 | Andrew Knackstedt |