80

I get the Bad Request- Invalid Hostname [HTTP ERROR 400] while trying connect my laptop's localhost. Actually, I am learning to develop mobile web using jQuery Mobile. I want to see the layout of the web whether fit my phone size.

i have added the port number which given by the Visual Studio with this way:
Control Panel> Firewall> Advanced Settings> Inbounce Rules> New Rule.
Then, i get my IP address by typing "ipconfig" at Command Prompt.
After that, I use my Android phone browser (Mozilla 5.0) and enter the link 192.XXX.XXX.XXX:57976.

I have read similar questions but I can't get the solution. Some people think this error is related to the IIS settings. But I can't get the solution of the IIS settings. Hope you guys can help me solve this problem.

sky91
  • 3,060
  • 2
  • 19
  • 26
  • 1
    You can save yourself a lot of bother with config files if you use our free VS Extension 'Conveyor' that opens up IIS Express to other devices https://marketplace.visualstudio.com/items?itemName=vs-publisher-1448185.ConveyorbyKeyoti – Jim W Apr 12 '18 at 17:33

3 Answers3

146

Step 1: Add Inbound Rule of Windows Firewall

Windows Firewall

  1. Open the Windows Firewall with Advanced Security.
  2. At the left panel, right click on Inbound Rule > New Rule
  3. Rule Type: Port
    Protocol and Ports: TCP
    Specific local ports: 57976
    Action: Allow the connection
    Profile: Tick all (Domain, Private, Public)
    Name: Name, Description(optional)
  4. Finish.

Step 2: IIS or IIS Express

Add Bindings of IIS Manager

IIS Manager

  1. Open the IIS Manager.
  2. At the left panel, Go to Sites > Default Web Sites.
  3. At the right panel, click on the Bindings. Then, the new dialog pop ups.
  4. At the pop-up dialog, click on the "Add" buttons. Input the port number and the hostname.
    For my case,
    port = 57976;
    hostname = 192.XXX.XXX.XXX (my IP address)

Add Bindings of IIS Express (Visual Studio)

IIS Express

  1. Stop the current Site
  2. For Visual Studio 2015, change the IIS config under your project folder C:\Projects\<ProjectName>\.vs\config\applicationhost.config‌​.
  3. For previous version of Visual Studio 2015, change the IIS config under IIS Express folder C:\Users\<your profile name>\Documents\IISExpress\config\applicationhost.config
  4. In applicationhost.config, search by the port number (for my case is 57976), then one more binding with your IP Address

        <site name="Web(1)" id="9">
          <application path="/" applicationPool="Clr4IntegratedAppPool">
            <virtualDirectory path="/" physicalPath="E:\abc\project\dev\web" />
          </application>
          <bindings>
            <binding protocol="http" bindingInformation="*:57976:localhost" />
            <binding protocol="http" bindingInformation="*:57976:192.XXX.X.XXX" />
          </bindings>
        </site>
    

Updated:

For windows 10 or Visual Studio 2015 users, you may get the error message below:

Unable to launch the IIS Express Web server, Failed to register URL, Access is denied

Solution:

  1. Close Visual Studio
  2. Right click on Visual Studio > Run as Administrator



Reference: https://azure.microsoft.com/en-us/documentation/articles/mobile-services-dotnet-backend-how-to-configure-iis-express/

sky91
  • 3,060
  • 2
  • 19
  • 26
  • 10
    An update on this. With the new ASP.NET Core the IIS config file is now inside the project folder so it would be something like "C:\Projects\\.vs\config\applicationhost.config". The .vs is a hidden folder. Also with windows 10 and Visual Studio 2015 you might have to set IIS Tray and IIS Express worker process to run as administrator, then Visual Studio will have to be started as Administrator since it starts these processes. They can be found here: C:\Program Files (x86)\IIS Express\iisexpresstray.exe and C:\Program Files (x86)\IIS Express\iisexpress.exe – John May 13 '16 at 19:57
  • 2
    need to edit the applicationhost.config in the .vs file which inside the project folder. It is hidden, so need to unhide to change it. – kyorilys Aug 31 '16 at 02:11
  • You don't actually need the IIS Express (Visual Studio) part. IIS Express isn't accessible from "outside" machines. The best workflow I've found is to select Local IIS from the web project properties Web section, and allowing Visual Studio to create a virtual directory. That way you can just hit play to debug the server API. You still need to make sure port 80 is open in the firewall rules though. – Josh Russo Jan 16 '17 at 14:21
  • After I do this, I get an error message "Unable to connect to web server 'IIS Express'" when I try to run the application. What could be the problem? – Zvonimir Matic Apr 04 '17 at 22:23
  • @ZvonimirMatic refer my `Updated` section. You need to run Visual Studio as Admin by `Right click on Visual Studio > Run as Administrator` – sky91 Apr 05 '17 at 01:43
  • Sadly doing this causes visual studio to just show `Unable to connect to web server 'IIS Express'`. Even after reverting any changes to the config. And persists till the config is deleted. – Douglas Gaskell Apr 13 '17 at 02:08
  • 2
    Didn't work for me. Windows 10, VS15, IIS Express serving https (may that be the problem?). Following instructions. Run as Administrator ecc... Changing the IIS config results in it be 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: "https://192.168.0.2:44324/" results in "Unable to launch the IIS Express web server" as already noted by others. – Luke Apr 25 '17 at 11:39
  • @Luke, I am using Windows 10, Visual Studio 2015 Pro but I didn't face any issue so far. What do mean by `Changing the IIS config results in it be overwritten adding an entire new section`? You open the `.vs\config\applicationhost.config‌​` which I mentioned in my answer. Just add the binding for your IP with localhost i.e add this `` It is not needed to override nor add new entire new `
    `. Try if it is working.
    – sky91 Apr 25 '17 at 17:10
  • can someone explain what the default website of IIS have in common with my debuging website? – ihavenokia May 18 '17 at 11:26
  • @MatheusMiranda Could you show me the url? This is the IIS default image. It means you are connected to the IIS but not your web application. – sky91 May 23 '17 at 03:17
  • I am using Visual Studio 2017 and url: 192.XXX.X.XXX:56987 – Matheus Miranda May 23 '17 at 03:20
0

Sometimes this error goes back to Cors (Cross-Origin Resource Sharing) setting in iis or web application

-9

Clear Browser data ctrl+shift+del

Syscall
  • 19,327
  • 10
  • 37
  • 52