C# – Can’t access WEB API with ip:port but can with localhost:port during VS debug mode

androidasp.netcdebugginglocalhost

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?

Best Answer

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.

Related Topic