In this article I will explain with an example, how to get the visitor’s Geographic Location (GeoLocation) details like Country, City, Region, Zip Code, Latitude, Longitude and Time zone using the Client IP Address of his machine in ASP.Net MVC.
The Visitor’s Geographic Location (GeoLocation) will be determined with the help of IPInfoDB IP to Location API in ASP.Net MVC.
Note: For beginners in ASP.Net MVC, please refer my article ASP.Net MVC Hello World Tutorial with Sample Program example.
 
 

The IP Address to Location API

The IPInfoDB IP to Location API is a FREE API to get the Geographic location details such as Country, City, Region, Zip Code, Latitude, Longitude and Time zone from IP Address.
In order to use the API, you will need to register and get an API Key. Once the API Key has been generated the API can be accessed using the following URL.
http://api.ipinfodb.com/v3/ip-city/?key=&ip=74.125.45.100&format=json
The API returns Geographic location details in JSON format as shown below.
{
  "statusCode" : "OK",
  "statusMessage" : "",
  "ipAddress" : "74.125.45.100",
  "countryCode" : "US",
  "countryName" : "UNITED STATES",
  "regionName" : "CALIFORNIA",
  "cityName" : "MOUNTAIN VIEW",
  "zipCode" : "94043",
  "latitude" : "37.3861",
  "longitude" : "-122.084",
  "timeZone" : "-07:00"
}
 
 

Model

The Model class consists of following properties.
public class LocationModel
{
    public string IPAddress { get; set; }
    public string CountryName { get; set; }
    public string CountryCode { get; set; }
    public string CityName { get; set; }
    public string RegionName { get; set; }
    public string ZipCode { get; set; }
    public string Latitude { get; set; }
    public string Longitude { get; set; }
    public string TimeZone { get; set; }
}
 
 

Namespaces

You will need to import the following namespaces.
usingSystem.Net;
usingSystem.Web.Script.Serialization;
 
 

Controller

The Controller consists of following Action method.

Action method for handling GET operation

Inside this Action method, first the IP Address is determined using HTTP_X_FORWARDED_FOR.
Note: For more details on how to get client IP Address of visitors machine in ASP.Net MVC, please refer my article How to get Client IP Address of Visitors Machine in ASP.Net MVC.
 
Then, a check is performed if IP Address is NULL or empty then, IP Address is set using REMOTE_ADDR and the LocationModel class object is created.
Next, the IP Address is then sent as parameter along with the API Key to the IPInfoDB IP to Location API which is being called using the WebClient class object.
Finally, the received JSON is deserialized into the LocationModel class object using the Deserialize method of JavaScriptSerializer class and returned to the View.
public class HomeController : Controller
{
    // GET: Home
    public ActionResult Index()
    {
        string APIKey "<Your API Key>";
        string ipAddress Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
        if (string.IsNullOrEmpty(ipAddress))
        {
            ipAddress Request.ServerVariables["REMOTE_ADDR"];
        }
 
        LocationModel location = new LocationModel();
        string url = string.Format("http://api.ipinfodb.com/v3/ip-city/?key={0}&ip={1}&format=json", APIKey, ipAddress);
        using (WebClient client = new WebClient())
        {
            string json client.DownloadString(url);
            location = new JavaScriptSerializer().Deserialize<LocationModel>(json);
        }
 
        return View(location);
    }
}
 
 

View

HTML Markup

Inside the View, in the very first line the LocationModel class is declared as model for the View.
The fetched visitor’s Geographic Location (GeoLocation) details like Country, City, Region, Zip Code, Latitude, Longitude and Time zone are displayed in the View.
@model IPAddress_Location_MVC.Models.LocationModel
 
@{
    Layout = null;
}
 
<!DOCTYPE html>
 
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <table cellpadding="0" cellspacing="0">
        <tr>
            <th>IP Address</th>
            <th>Country</th>
            <th>City</th>
            <th>Region</th>
            <th>Country Code</th>
            <th>Latitude</th>
            <th>Longitude</th>
            <th>Timezone</th>
        </tr>
        <tr>
            <td>@Model.IPAddress</td>
            <td>@Model.CountryName</td>
            <td>@Model.CityName</td>
            <td>@Model.RegionName</td>
            <td>@Model.CountryCode</td>
            <td>@Model.Latitude</td>
            <td>@Model.Longitude</td>
            <td>@Model.TimeZone</td>
        </tr>
    </table>
</body>
</html>
 
 

Screenshot

Find Visitors Geographic Location (GeoLocation) using IP Address in ASP.Net MVC
 
 

Downloads