public static GeoLiteCityLocationViewModel GetGeoLiteCityLocation(IPAddress ipAddress) { var dic = GeoCityReader.Find <Dictionary <string, object> >(ipAddress); var geo = new GeoLiteCityLocationViewModel(); geo.LocId = GetValue(dic, "city", "geoname_id"); geo.Country = GetValue(dic, "country", "iso_code"); geo.Region = GetValue(dic, "continent", "code"); geo.City = GetValue(dic, "city", "names", "en"); geo.PostalCode = string.Empty; geo.Latitude = Convert.ToDouble(GetValue(dic, "location", "latitude")); geo.Longitude = Convert.ToDouble(GetValue(dic, "location", "longitude")); geo.MetroCode = string.Empty; geo.AreaCode = string.Empty; geo.IsAnonymousIP = CheckAnonymousIP(ipAddress); return(geo); }
public IHttpActionResult Get(string ip = "") { if (string.IsNullOrEmpty(ip)) { ip = GetIpAddress(); } ip = RemovePort(ip); IPAddress ipAddress; if (string.IsNullOrEmpty(ip) || !IPAddress.TryParse(ip, out ipAddress)) { return(BadRequest("Invalid ip address")); } GeoLiteCityLocationViewModel geoLiteCityLocation = null; if (ConfigHelper.UseMaxMindDb()) { geoLiteCityLocation = MaxMindDatabaseHelper.GetGeoLiteCityLocation(ipAddress); } else { geoLiteCityLocation = CsvDatabaseHelper.GetGeoLiteCityLocation(ip); } if (geoLiteCityLocation == null) { return(BadRequest("Invalid ip address")); } geoLiteCityLocation.Ip = ip; return(Ok(geoLiteCityLocation)); }