In this article I will explain with an example, how to get the Visitor’s Geographic location details like Country, City, Region, Zip Code, Latitude, Longitude and Time zone using IP Address in ASP.Net using C# and VB.Net.
First the IP Address of the Visitor will be determined and then using the IP Address and the IPInfoDB IP to Location API, the Visitor’s Geographic location details will be determined in ASP.Net using C# and VB.Net.
 
 
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.
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"
}
 
 
HTML Markup
The following HTML Markup consists of an ASP.Net GridView which will be used to display the Geographic location details.
<asp:GridView ID="gvLocation" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField DataField="IPAddress" HeaderText="IP Address" />
        <asp:BoundField DataField="CountryName" HeaderText="Country" />
        <asp:BoundField DataField="CityName" HeaderText="City" />
        <asp:BoundField DataField="RegionName" HeaderText="Region" />
        <asp:BoundField DataField="CountryCode" HeaderText="Country Code" />
        <asp:BoundField DataField="Latitude" HeaderText="Latitude" />
        <asp:BoundField DataField="Longitude" HeaderText="Latitude" />
        <asp:BoundField DataField="Timezone" HeaderText="Timezone" />
    </Columns>
</asp:GridView>
 
 

Namespaces
You will need to import the following namespaces.
C#
using System.Net;
using System.Collections.Generic;
using System.Web.Script.Serialization;
 
VB.Net
Imports System.Net
Imports System.Collections.Generic
Imports System.Web.Script.Serialization
 
 
Business Class to hold the JSON data
The following properties class Location will be required to hold the Geographic location details returned from the API.
C#
public class Location
{
    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; }
}
 
  VB.Net
Public Class Location
    Public Property IPAddress() As String
    Public Property CountryName() As String
    Public Property CountryCode() As String
    Public Property CityName() As String
    Public Property RegionName() As String
    Public Property ZipCode() As String
    Public Property Latitude() As String
    Public Property Longitude() As String
    Public Property TimeZone() As String
End Class
 
 
Fetching and displaying the Location Details for the IP Address
Inside the Page Load event, first the IP Address is determined.
Note: For more details on fetching IP Address, please refer my article How to get IP Address of Visitors Machine in ASP.Net.
 
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.
The received JSON is deserialized into the Location class object which is finally used to populate the ASP.Net GridView control.
C#
protected void Page_Load(object sender, EventArgs e)
{
    string ipAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
    if (string.IsNullOrEmpty(ipAddress))
    {
        ipAddress = Request.ServerVariables["REMOTE_ADDR"];
    }
 
    string APIKey = "<Your API Key>";
    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 location = new JavaScriptSerializer().Deserialize<Location>(json);
        List<Location> locations = new List<Location>();
        locations.Add(location);
        gvLocation.DataSource = locations;
        gvLocation.DataBind();
    }
}
 
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    Dim ipAddress As String = Request.ServerVariables("HTTP_X_FORWARDED_FOR")
    If String.IsNullOrEmpty(ipAddress) Then
        ipAddress = Request.ServerVariables("REMOTE_ADDR")
    End If
 
    Dim APIKey As String = "<Your API Key>"
    Dim url As String = String.Format("http://api.ipinfodb.com/v3/ip-city/?key={0}&ip={1}&format=json", APIKey, ipAddress)
    Using client As New WebClient()
        Dim json As String = client.DownloadString(url)
        Dim location As Location = New JavaScriptSerializer().Deserialize(Of Location)(json)
        Dim locations As New List(Of Location)()
        locations.Add(location)
        gvLocation.DataSource = locations
        gvLocation.DataBind()
    End Using
End Sub
 
 
Screenshot
Find Visitors Geographic Location using IP Address in ASP.Net
 
 
Demo
 
 
Downloads