Geographical Information by IP Address in ASP.NET and C#

There are a lot of services out there by using them we can get a Website Visitor Geographical information such as country, region, city, latitude, longitude, ZIP code, time zone and so on. It is possible just by using the IP address of website visitor's machine.  


Here, in this article, I am using a service provided by
http://iplocationtools.com/. This service is free and based on the IP, returns geographical data in 3 formats:

1. XML

2. JSON

3. CSV

 

This is the aspx code:

 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title>Ip Address Location</title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

        <asp:ScriptManager ID="ScriptManager1" runat="server">

        </asp:ScriptManager>

        <table cellpadding="0" cellspacing="0" width="50%" align="center" style="background-color: #f5f5f5;

            color: #4f6b72;">

            <tr>

                <td align="center">

                    <asp:UpdatePanel ID="UpdatePanel1" runat="server">

                        <ContentTemplate>

                            <asp:Panel ID="panelLoc" runat="server">

                                <asp:TextBox ID="txtIP" runat="server"></asp:TextBox>

                                <asp:Button ID="btnGetLoc" runat="server" Text="Get IP Details" OnClick="btnGetLoc_Click" />

                                <br />

                                <asp:Xml ID="Xml1" runat="server"></asp:Xml>

                            </asp:Panel>

                        </ContentTemplate>

                    </asp:UpdatePanel>

                    <asp:UpdateProgress ID="updProgress" AssociatedUpdatePanelID="UpdatePanel1" runat="server">

                        <ProgressTemplate>

                            <img alt="progress" src="images/Img.gif" />

                        </ProgressTemplate>

                    </asp:UpdateProgress>

                </td>

            </tr>

        </table>

    </div>

    </form>

</body>

</html>

 


Here is the code behind.

 

using System;

using System.Xml.Linq;

 

public partial class _Default : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

 

    }

 

    protected void btnGetLoc_Click(object sender, EventArgs e)

    {

        String url = String.Empty;       

 

        if (txtIP.Text.Trim() != String.Empty)

        {

            url = String.Format("http://iplocationtools.com/ip_query2.php?ip={0}", txtIP.Text.Trim());

            XDocument xDoc = XDocument.Load(url);

            if (xDoc == null | xDoc.Root == null)

            {

                throw new ApplicationException("Data is not Valid");

            }

 

            Xml1.TransformSource = "IP.xslt";

            Xml1.DocumentContent = xDoc.ToString();

        }      

    }

}

 

 

Here, I also observe that a XML control (<asp:Xml ID="Xml1") has been added to the page.

Our XSLT will look similar to the following:

<?xml version='1.0'?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

  <xsl:template match="/">

    <HTML>

      <BODY>

        <TABLE cellspacing="4" cellpadding="4">

          <xsl:for-each select="Locations/Location">

            <TR>

              <TD width="5%" bgcolor="#3BB9FF">

                <B>Ip</B>

              </TD>

              <TD width="5%" valign="top" bgcolor="#48CCCD">

                <xsl:value-of select="Ip"/>

              </TD>

              <TD></TD>

            </TR>

            <TR>

              <TD width="5%" bgcolor="#3BB9FF">

                <B>Status</B>

              </TD>

              <TD width="5%" valign="top" bgcolor="#48CCCD">

                <xsl:value-of select="Status"/>

              </TD>

              <TD></TD>

            </TR>

            <TR>

              <TD width="5%" bgcolor="#3BB9FF">

                <B>CountryCode</B>

              </TD>

              <TD width="5%" valign="top" bgcolor="#48CCCD">

                <xsl:value-of select="CountryCode"/>

              </TD>

              <TD></TD>

            </TR>

            <TR>

              <TD width="5%" bgcolor="#3BB9FF">

                <B>CountryName</B>

              </TD>

              <TD width="5%" valign="top" bgcolor="#48CCCD">

                <xsl:value-of select="CountryName"/>

              </TD>

              <TD></TD>

            </TR>

            <TR>

              <TD width="5%" bgcolor="#3BB9FF">

                <B>RegionCode</B>

              </TD>

              <TD width="5%" valign="top" bgcolor="#48CCCD">

                <xsl:value-of select="RegionCode"/>

              </TD>

              <TD></TD>

            </TR>

            <TR>

              <TD width="5%" bgcolor="#3BB9FF">

                <B>RegionName</B>

              </TD>

              <TD width="5%" valign="top" bgcolor="#48CCCD">

                <xsl:value-of select="RegionName"/>

              </TD>

              <TD></TD>

            </TR>

            <TR>

              <TD width="5%" bgcolor="#3BB9FF">

                <B>City</B>

              </TD>

              <TD width="5%" valign="top" bgcolor="#48CCCD">

                <xsl:value-of select="City"/>

              </TD>

              <TD></TD>

            </TR>

            <TR>

              <TD width="5%" bgcolor="#3BB9FF">

                <B>ZipPostalCode</B>

              </TD>

              <TD width="5%" valign="top" bgcolor="#48CCCD">

                <xsl:value-of select="ZipPostalCode"/>

              </TD>

              <TD></TD>

            </TR>

            <TR>

              <TD width="5%" bgcolor="#3BB9FF">

                <B>Latitude</B>

              </TD>

              <TD width="5%" valign="top" bgcolor="#48CCCD">

                <xsl:value-of select="Latitude"/>

              </TD>

              <TD></TD>

            </TR>

            <TR>

              <TD width="5%" bgcolor="#3BB9FF">

                <B>Longitude</B>

              </TD>

              <TD width="5%" valign="top" bgcolor="#48CCCD">

                <xsl:value-of select="Longitude"/>

              </TD>

              <TD></TD>

            </TR>

          </xsl:for-each>

        </TABLE>

      </BODY>

    </HTML>

  </xsl:template>

</xsl:stylesheet>

When you run the application and enter an IP address, you will data something like this.



IpLocation.JPG


Image 1.


Now if you wish to get geographical information of your website visitors, you can simply get IP address of a visitor from the Browser and pass that IP address in this service and get the desired data you want.




Similar Articles