Step for impelmentation:
1. Create class Location
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; }
}
2. Create Web form FetchGeoIPData_IPAddress.aspx
<asp:gridview id="gvLocation" runat="server" autogeneratecolumns="False" borderwidth="1px"
backcolor="#DEBA84" cellpadding="3" cellspacing="2" borderstyle="None" bordercolor="#DEBA84">
<FooterStyle ForeColor="#8C4510" BackColor="#F7DFB5"></FooterStyle>
<PagerStyle ForeColor="#8C4510" HorizontalAlign="Center"></PagerStyle>
<HeaderStyle ForeColor="White" Font-Bold="True" BackColor="#A55129"></HeaderStyle>
<Columns>
<asp:BoundField DataField="IPAddress" HeaderText="IP Address" ControlStyle-Width="200px"/>
<asp:BoundField DataField="CountryName" HeaderText="Country" />
<asp:BoundField DataField="CountryCode" HeaderText="Country Code" />
<asp:BoundField DataField="CityName" HeaderText="City" />
<asp:BoundField DataField="RegionName" HeaderText="Region" />
<asp:BoundField DataField="Latitude" HeaderText="Latitude" />
<asp:BoundField DataField="Longitude" HeaderText="Latitude" />
<asp:BoundField DataField="Timezone" HeaderText="Timezone" />
</Columns>
</asp:gridview>
3. Add the following namespace:
using System.Net;
using System.Collections.Generic;
using System.Web.Script.Serialization;
4. On the FetchGeoIPData_IPAddress.aspx.cs code-behind file
Add this code to btnGetGeoIPData_Click event
protected void btnGetGeoIPData_Click(object sender, EventArgs e)
{
string ipAddress = txtIPAddress.Text;
if (string.IsNullOrEmpty(ipAddress))
{
ipAddress = Request.ServerVariables["REMOTE_ADDR"];
}
string APIKey = "<Chane with Your API>";
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();
}
}