Bad Request - Invalid Hostname with Asp.net WebAPI project in Visual Studio 2013

53,478

Solution 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

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

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

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.

Solution 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.

Share:
53,478

Related videos on Youtube

Frank Visaggio
Author by

Frank Visaggio

Sr. Staff Engineer, Human Factors + User Experience Design. Formerly Software Developer / Cloud Architect with a passion for Human-Computer Interaction.

Updated on April 20, 2022

Comments

  • Frank Visaggio
    Frank Visaggio about 2 years

    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)

    • Nanhydrin
      Nanhydrin about 9 years
      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.
    • Frank Visaggio
      Frank Visaggio about 9 years
      yes I can run it using the hostname instead of localhost however externally i get this error
    • Frank Visaggio
      Frank Visaggio about 9 years
      solved this but i still dont understand the binding protocol well
    • Nanhydrin
      Nanhydrin about 9 years
      What was the solution in the end?
    • Frank Visaggio
      Frank Visaggio about 9 years
      updated with the answer (it was a networking gotcha that slipped my mind)
    • Jim W says reinstate Monica
      Jim W says reinstate Monica about 6 years
      Our free VS extension would help, it opens up IIS Express without config changes marketplace.visualstudio.com/…
    • German Gracia
      German Gracia almost 3 years
  • John Henckel
    John Henckel about 9 years
    Thanks! very helpful +1
  • Frank Visaggio
    Frank Visaggio about 9 years
    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
  • EK_AllDay
    EK_AllDay over 8 years
    this does not work for visual studio 2015. Any updates?
  • Frank Visaggio
    Frank Visaggio over 8 years
    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.
  • vincent gravitas
    vincent gravitas about 8 years
    I could not find any other sources for help or documentation on this separate config file. Thank you for pointing it out.
  • ComeIn
    ComeIn about 8 years
    This is a big file with lots of sections. Any clue on which section this line goes in?
  • dnanon
    dnanon about 8 years
    when i change the line from ":<port>:localhost" to ":<port>:*" i get an exception "Invalid URI: The hostname could not be parsed." Im on Vs2015U2
  • Klaus Rubba
    Klaus Rubba almost 8 years
    like dnanon, i keep getting "Invalid URI" error when i try editing the applicationhost.config file. I've tried ":<port>.*" and ":<port>.", i've tried adding a line or replacing the line. I feel like I'm so close!
  • Anup Sharma
    Anup Sharma over 7 years
    You sir saved my so much time. Thank you.
  • user2023861
    user2023861 about 7 years
    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.
  • Luke
    Luke about 7 years
    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".
  • lee_mcmullen
    lee_mcmullen over 5 years
    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
  • Frank Visaggio
    Frank Visaggio almost 4 years
    can you give us any details about the versions you were running and the OS?
  • user1306322
    user1306322 almost 4 years
    @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)