Hi
I want to get User's Location based on IP. When user enters website
I used to do it with XMLHTTPREquest in classic asp
how to do it with .net MVC.
I am new to .net
Hi
I want to get User's Location based on IP. When user enters website
I used to do it with XMLHTTPREquest in classic asp
how to do it with .net MVC.
I am new to .net
A combination of WebClient and JavaScriptSerializer classes could help you. As always start by defining a class that will represent your model:
public class LocationResult
{
public string Ip { get; set; }
public string Status { get; set; }
public string CountryCode { get; set; }
public string CountryName { get; set; }
public string RegionCode { get; set; }
public string RegionName { get; set; }
public string City { get; set; }
public string ZipPostalCode { get; set; }
public float Latitude { get; set; }
public float Longitude { get; set; }
}
and then call the service and deserialize the JSON result back to your model:
public LocationResult GetLocationInfo(string ip)
{
using (var client = new WebClient())
{
// query the online service provider and fetch the JSON
var json = client.DownloadString(
"http://ipinfodb.com/ip_query.php?ip=" + ip +
"&output=json&timezone=false"
);
// use the JavaScriptSerializer to deserialize the JSON
// result back to a LocationResult
var serializer = new JavaScriptSerializer();
return serializer.Deserialize<LocationResult>(json);
}
}