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

后端 未结 4 510
甜味超标
甜味超标 2020-12-25 14:11

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

相关标签:
4条回答
  • 2020-12-25 14:37

    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.

    0 讨论(0)
  • 2020-12-25 14:39

    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

    0 讨论(0)
  • 2020-12-25 14:39

    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.

    0 讨论(0)
  • 2020-12-25 14:52

    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
    
    0 讨论(0)
提交回复
热议问题