27

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)

7
  • On the local machine, can you access the site using the computer name rather than localhost? Visual studio normally doesn't run as an admin, just right click on it and Run as Administrator to launch it same as you would for anything else.
    – Nanhydrin
    Commented Feb 24, 2015 at 17:03
  • 1
    yes I can run it using the hostname instead of localhost however externally i get this error Commented Feb 24, 2015 at 20:13
  • 1
    solved this but i still dont understand the binding protocol well Commented Feb 24, 2015 at 21:15
  • What was the solution in the end?
    – Nanhydrin
    Commented Feb 25, 2015 at 9:21
  • updated with the answer (it was a networking gotcha that slipped my mind) Commented Feb 25, 2015 at 21:29

6 Answers 6

40

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

8
  • 2
    Awesome glad it helped @JohnHenckel. A lot of times I question answering my own questions vs deleting them because I think what are the odds someone had a similar problem. Glad to see someone else got some benefit from it Commented May 7, 2015 at 17:22
  • 3
    this does not work for visual studio 2015. Any updates?
    – EK_AllDay
    Commented Dec 20, 2015 at 22:13
  • 1
    sadly no I am using the same code in VS2013 and 2015 and it seems to work for me. Maybe your running a different version of IISExpress and the applicationhost.config format is different @EthanKeiser. Also i typically install 2013 first and then 2015 so maybe my version of IISExpress is a version behind yours. Commented Feb 16, 2016 at 21:43
  • 3
    Try running Visual Studio as an administrator. I was getting this error even after making the change to the application.config file. When I run Visual Studio as an administrator however, I get what I expect. Commented Mar 20, 2017 at 21:13
  • 2
    This doesn't work for me in VS2017 with a Core web app on IIS Express. What did work however was the same but without the asterisks i.e. <binding protocol="http" bindingInformation=":53676:" /> as detailed here: stackoverflow.com/a/48900559/842238 Commented Nov 7, 2018 at 10:52
23

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
2
  • I could not find any other sources for help or documentation on this separate config file. Thank you for pointing it out. Commented Apr 23, 2016 at 21:32
  • Didn't work for me. Windows 10, VS15, IIS Express serving https (may that be the problem?). Following instructions. Changing the IIS config results in it be automatically overwritten adding an entire new section "site name='mywebsite(x)'. with 'x' as a progressive int and with old "localhost" address any time I start the website. Modifying from VS15 (website properties) web > project url: "192.168.0.2:44324/"; results in "Unable to launch the IIS Express web server".
    – Luke
    Commented Apr 25, 2017 at 11:43
4

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.

2
  • can you give us any details about the versions you were running and the OS? Commented Jun 1, 2020 at 16:38
  • @FrankVisaggio at the time of writing this answer, I was using the latest Windows 10 Pro (regular, not fast ring update) and Visual Studio 2019 (general availability release, not Preview) Commented Jun 19, 2020 at 18:20
3

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.

2

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:

  1. Open Visual Studio Developers Tools as Administrator
  2. 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
  3. netsh http show sslcert ipport=0.0.0.0:44302 (you will need to get appid from this step)
  4. 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".

  1. netsh http add sslcert ipport=0.0.0.0:44302 certhash=<thumbprint> appid={id from step 3} (include bracets {})

Hope that helps.

1

simply adding allowed hosts to * in appsettings.local.json solved my issue & I can see swagger page.

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*" //<-- this one
}

However, the secure connection warning is still there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.