45

I am trying to write an WEB API from .net and trying for my Android Application to query some data from the sql server database.

I have the web api written and it works well in debug mode.

My question is I notice the url of that application is localhost:port and it runs fine. However, when I try to change it to MYIP:port (eg. http:192.168.X.1234) or MYHOSTNAME:port (eg win7home:1234) this gives me Bad Request - Invalid Hostname.

I know I can deploy this to IIS and my IIS is setup but I was just wondering how come it doesn't work in debug mode???

Is there a way for me to run it in debug mode and test in on my Android at the same time instead of having to deploy it every time I want to make a change?

Ivan Pavičić
  • 1,083
  • 2
  • 16
  • 28
noobiehacker
  • 1,079
  • 2
  • 12
  • 24

6 Answers6

48

If you're running it in debug mode I assume you're using IIS-Express.

By default, IIS-Express only binds to localhost.

To circumvent this, you can open the IIS-Express application config file located at: C:\Users\<username>\My Documents\IISExpress\config\applicationhost.config and modify the site's binding information.

change

<binding protocol="http" bindingInformation="*:55284:localhost" />

to

<binding protocol="http" bindingInformation="*:55284:*" />

You'll also have to restart IIS-Express after the change.

Matthew
  • 21,467
  • 5
  • 66
  • 95
  • 8
    As stated [here](https://johan.driessen.se/posts/Accessing-an-IIS-Express-site-from-a-remote-computer), as of Visual Studio 2015, the config files are per project and stored in `/{project folder}/.vs/config/applicationhost.config` – Matthias Jun 27 '18 at 05:35
21

Both Anton and Matthew's Answers pointed me to the right direction

So this what I did

  1. Run Visual Studios in administrator mode

  2. Changed the binding protocols and allow for incoming directions as suggested http://johan.driessen.se/posts/Accessing-an-IIS-Express-site-from-a-remote-computer

    But after that, I have a service unavailable (503) error

  3. So I followed this : IIS Express Enable External Request - 503 Added just the port protocol and port:ip protocol,

Than it works both on my machine's browser and on my phone.

Not too too sure why the 3rd step is needed -my hypothesis is (the localhost url is needed for VS to point to and the ip url is used for accessing from another machine)

Community
  • 1
  • 1
noobiehacker
  • 1,079
  • 2
  • 12
  • 24
  • Thank you! I just want to add that you need to restart IIS Express for the changes to take. The easiest way to do this was to right-click on the IIS Express icon and click exit. The icon will go away, so you can't restart it that way. Instead just start a debug session of your web app from Visual Studio. It will restart IIS. As a bonus, you can confirm the changes take by right-clicking on the IIS icon, finding your web app under "View Sites" and then the IP addresses from which you app is can now be reached. It should show your binding changes right there. – strangeluck Jul 24 '14 at 13:30
  • I stumbled across this question using self-hosted Web API. There is some more relevant information at http://stackoverflow.com/questions/16642651/self-hosted-owin-and-urlacl – Jedidja Aug 20 '14 at 15:02
  • 2
    What about iis (not iis express)? – Kira Sep 04 '14 at 13:25
  • 1
    @HediNaily In iis7 and up, you open up the IIS manager tool and then right click on the website and go to `edit bindings` – Matthew Jan 10 '15 at 04:11
  • I tried this but anytime I try editing the applicationhost.config file for my solution, I get a "Unable to connect to web server: IIS Express". The only thing that fixes it is deleting the .config file and letting vs create another but then I lose my bindings. – Hank Jul 11 '18 at 18:54
  • worked well, thanks ! The only problem is once you change the binding, VS can't debug without being run as admin :/ – Nk54 Apr 29 '19 at 13:25
  • @Hank, I am having the same problem – MoKG Jan 22 '20 at 10:28
7

I had the same problems when I wanted to share my localhost IIS so some guys could just type my machine name or IP and connect to my web app instance. So if this is what you need when http://johan.driessen.se/posts/Accessing-an-IIS-Express-site-from-a-remote-computer. It works for me for both Silverlight and MVC apps. I even set breakpoints and they are hit from a remote machine.

Anton
  • 741
  • 4
  • 5
  • and you have too use IIS Express – Anton Oct 24 '13 at 00:59
  • and PLEASE don't forget about security on your machine. The above article saved me a lot of time , because I didn't pay attention security policy – Anton Oct 24 '13 at 01:01
  • tried this. I get an error saying "unable to connect to web server IIS Express". It's fine when I change it back to localhost – MoKG Jan 22 '20 at 10:26
5

In your solution dir, in the file .vs\config\applicationHost.config change the line

 <binding protocol="http" bindingInformation="*:1234:localhost" />

to

<binding protocol="http" bindingInformation=":1234:192.168.1.35" />

and run Visual Studio as an admin. Restart IIS Express. this solution work for me.

Ahmad Aghazadeh
  • 14,753
  • 10
  • 88
  • 89
0

Had the same issue debugging in Visual Studio Code, I solved it by adding:

"env": {
      // ...
      "ASPNETCORE_URLS": "http://*:5000" // change to the port you are using
      // ...
},

.. to launch.json

Apparently, by default it binds http protocol to 'localhost:5000', so it works with localhost but not with ip address.

If you are trying to hit a breakpoint by a request coming from a different computer, don't forget to check your firewall settings (and/or antivirus)

hope this helps

0

I have been looking for this a while and finally found a solution for people who debug to console app (launch: project).

In the Program.cs class you have to add the UseUrls()

public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder
                       .UseUrls("http://0.0.0.0:5000", "https://0.0.0.0:5001")
                       .UseStartup<Startup>();
                });

I couldnt find this anywhere online but I found it in some old project I had where it was working.