Click here to Skip to main content
15,501,351 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have an ASP.NET website written in C#.

On this site I need to automatically show a start page based on the user's location.

Can I get name of user's city based on the IP address of the user ?
Updated 5-Jan-17 16:51pm

You need reverse geo-coding API based on IP address. Check these:
Get user location by IP address[^]
Getting ipaddress and location of every user visiting your website[^]
Share this answer
bjdestiny 25-Nov-13 1:35am    
I liked this... Bookmarked it & +5..
ridoy 25-Nov-13 12:55pm    
thank you,:)
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web;
using System.Text;
using System.IO;
using System.Xml;
using System.Net;
using System.Net.Sockets;

public partial class _Default : System.Web.UI.Page 
    protected void Page_Load(object sender, EventArgs e)
        //this line is to check the clien ip address from the server itself
        string IP = "";

        string strHostName = "";
        strHostName = System.Net.Dns.GetHostName();

        IPHostEntry ipEntry = System.Net.Dns.GetHostEntry(strHostName);

        IPAddress[] addr = ipEntry.AddressList;

        IP = addr[2].ToString();

        //Initializing a new xml document object to begin reading the xml file returned
        XmlDocument doc = new XmlDocument();           
        XmlNodeList nodeLstCity = doc.GetElementsByTagName("City");     
        IP = ""+ nodeLstCity[0].InnerText + "<br>" + IP;
        //this is my header that I love

[Edit:] Code block added
Share this answer
Codes DeCodes 26-Mar-14 7:08am    
i also needed this in my web application..i tried it.. as this code reaches, it shows me error "Unable to connect to the remote server".. i typed the address in my browser was opened and address of my city was opened.. now as i typed in browser , "RD Reserved" is shown... any help in what is happening??
veerendrasingh 19-Apr-16 8:43am    
is website is trustable?[^]

Request to this url and parse json to get user's country,city,IP ....
Share this answer
Even a simpler solution then you have implemented
string ip = Server.HtmlEncode(Request.UserHostAddress);

XmlDocument doc = new XmlDocument();

string getdetails= "" + ip;


XmlNodeList nodeLstCity = doc.GetElementsByTagName("City");

string location = nodeLstCity[0].InnerText;
Share this answer
veerendrasingh 19-Apr-16 8:43am is not working...
Thanks7872 20-Apr-16 2:28am    
It was working in 2013 :-)
Share this answer
I think you had not searched for your problem in CP.
Anyway just visit This[^], This1[^] and This[^] links you will get your solution.
Share this answer
Maddy selva 28-Nov-13 1:01am    
I have solved this my self!..
At you can request an IP address using cURL. It returns a few objects including location, timezone and datetime.


- Location including latitude / longitude
- timezone (time zone variables e.g. capital, currency, phone prefix, shortcodes etc.
- datetime (the date / time for the timezone when the request was made + lots of date / time variables).

Take a look here for API documentation:
IP address. Get users location, time zone and date/time details. - Timezone API[^]
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900