I am running an ASp.Net MVC app in the localhost - dev server given with a visual studio. I want to get the IP address. I tried
Request.UserHostAddress
and
Request.ServerVariables("REMOTE_ADDR")
In both cases, I am getting::1 as a result. What is it? Why am I getting it? How can I get 127.0.0.1 or 192.168.1.xxx?
instead of
Request.ServerVariables("REMOTE_ADDR")
If you want localhost return 127.0.0.1, maybe you need to change your "hosts" file. You can find it in "%systemdrive%\Windows\System32\drivers\etc"
It works for me, now I get 127.0.0.1 with "Request.ServerVariables["REMOTE_ADDR"]". I uncomment 127.0.0.1 (remove #).
Here you can find default hosts file http://support.microsoft.com/kb/972034
My file
# localhost name resolution is handled within DNS itself.
# ::1 localhost
What you're seeing when calling 'localhost' is valid. ::1 is the IPv6 loopback address. Equivalent to 127.0.0.1 for IPv4.
Instead of calling:
Call:
[Replace {machinename} with your machine's computer name. Replace XXX with your computer's IP address.]
Anyone calling into your machine to the MVC app will have their valid IP address as a result. If the client is an IPv6 host it will save there IPv6 IP address. If the client is an IPv4 host it will save there IPv4 IP address.
If you always want to save an IPv4 address take a look at this article on how they accomplished it with a simple class http://www.4guysfromrolla.com/articles/071807-1.aspx. You should be able to take there example and build a quick helper method to accomplish this.
below code i have used for finding ip
You are getting a valid IP Address.
::1
is local_host in IPv6. (underscore used in local_host to stop SO from thinking it was some sort of bad text)