C# Web - localhost:port works, 127.0.0.1:port doesn't work

Kevin Cruijssen picture Kevin Cruijssen · May 1, 2014 · Viewed 30.8k times · Source

I just finished adding C# Web API components (Web API Models & Controllers) to a localhost copy of an existing project.

This Web API's GET-methods should be called from an Android app. In this link it's explained I should use 10.0.2.2 on the Android Emulator to get the computer's 127.0.0.1.

When I did this, it didn't work for my HttpRequest in the Android app. So I went to the Android browser and typed it directly, and it also didn't work.

Then I tried using 127.0.0.1 instead of localhost in my computer's browser, and for some unknown reason it also doesn't work.. Is there a different between localhost and 127.0.0.1? I always thought they were one and the same.

Here is the 400 error I get when using 127.0.0.1: 127.0.0.1 error 400 bad request

And with localhost everything works fine.

So, my question: How can I use localhost on the Emulator (or, how can I fix the error I get when using 127.0.0.1 instead of localhost)? Also, I would like to know the difference between localhost and 127.0.0.1, since I always thought they were the same.

Thanks in advance for the responses.


Edit 1:

In this stackoverflow question they mention the host file in System32 of Windows. I opened this file with Notepad++ (as Administrator) and uncommented the lines with 127.0.0.1 localhost and ::1 localhost. But unfortunally this didn't fixed the problem and I still can't use 127.0.0.1 on my computer as a replacement for localhost. Probably because my problem is the reversed (I can access localhost, but not 127.0.0.1, instead of the other way around.)


Edit 2:

In this stackoverflow answer it's explained that the differences between localhost and 127.0.0.1 are:

  • 127.0.0.1 will be more easily recognized as an IP in some programming languages.
  • localhost can be changed to another IP in the computer's host-file (the file mentioned in my Edit 1).
  • There are some differences between IPv4 and IPv6.

All and all I kinda understand the differences now, I just don't get why my localhost is working, but 127.0.0.1 isn't..


Edit 3:

Does it have to do something with the port (I use 54408 as port)? I've opened cmd and did the following tests:

  • ping localhost: I'm getting a response with 4 times Reply from ::1: time<1ms.
  • ping 127.0.0.1: I'm getting a response with 4 times Reply from 127.0.0.1: bytes=32 time<1ms TIL=128
  • ping localhost:54408: I'm getting an error Ping request could not find localhost:54408. Please check the name and try again.
  • ping 127.0.0.1:54408: I'm getting an error Ping request could not find 127.0.0.1:54408. Please check the name and try again.

And like I've said before: 127.0.0.1:54408 in the browser gives the error seen in the image above. And localhost:54408 works just fine..

Still no one with an idea on how to fix this?


Edit 4:

Copy of my hosts-file, located in C:\Windows\System32\drivers\etc.

# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
#      102.54.94.97     rhino.acme.com          # source server
#       38.25.63.10     x.acme.com              # x client host

# localhost name resolution is handled within DNS itself.
127.0.0.1   localhost
::1         localhost

The last two lines used to be:

#      127.0.0.1     localhost
#      ::1           localhost

Edit 5 / Semi-Solution:

After Jake C's suggestion I went looking for Visual Studio port configuration and found the following site. At the section "To specify a port for a Web application project that uses the Visual Studio Development Server" I followed the instructions and changed the Web option in the Project Properties to Use Visual Studio Development Server with my 54408 port.

This was a great step in the right direction, since 127.0.0.1:54408 homepage now works. However, once I try to log in with the Google OAuth on the C# website, I was getting the following error: enter image description here

One of my ex-colleagues who worked on the C# web project told me once about this stackoverflow post. In the answer of this post is stated that I should add the redirect-urls (in my case 127.0.0.1) to the Google APIs Console. Right now I don't have access to this Console for my localhost project however, since it's obtained through SVN. I will ask to one of my supervisors for permission to view the Google APIs Console of this C# web project and perhaps edit the redirect-urls to include 127.0.0.1.

Once I've got it completely working I'll accept Jake C's answer, since his explanation of the Http port configurations did indeed help me find the answer.


Edit 6:

Ok, I've used my own Google APIs Console and created a new Project with a new Client ID. I've added both the http://localhost:54408/Account/ExternalLoginCallback and http:127.0.0.1:54408/Account/ExternalLoginCallback to the redirect-urls. Then in my C# web project's App_Start/AuthConfig.cs I've changed the Client settings to use this new client.

Once again thanks Jake C for your suggestion to change the http port configurations in Visual Studio. This did the trick for me.

Answer

Mick Owen picture Mick Owen · Dec 6, 2015

If you are using Visual Studio's built in web server (IIS Express), localhost is mapped by default; to enable 127.0.0.1:

1) At path: %USERPROFILE%\Documents\IISExpress\config

2) Locate config file: applicationhost.config

3) - open config file in editor (I use notepad++)

4) Search for the site port, for example if the url is typically localhost:57578, search "57578" and you should find:

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

5) Change this entry to:

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

6) Save and exit, restart website.

Note: You will want to repeat this process any time you create a new virtual directory (changing the port number Project/Properties/Web/Project Url), which creates a new entry in the applicationhost.config file.