Bean Software Logo
ASP.NET Database Search Control
 Home
    Shop
    Advertise
    Write For Us
    Affiliate
    Newsletter
    Contact

FREE Geo Targeting ASP.NET Component

Geo Targeting component is free ASP.NET component based on C# API of MaxMind. With Geotargeting component you can find out more about location of your web site visitor, like visitor's city or country. You can use this information to display more relevant ads or content to your users.

Free Geo Targeting component features

Component can use free GeoLite City database or paid GeoIP City database which is more accurate. You can get these data about your visitor location:

- Country name

- Country code

- Region

- City

- Postal code

- Area code

- Dma

- Latitude

- Longitude

How to implement geo targeting on your site

To implement geo targeting on your ASP.NET web site, follow these three simple steps:

1. Set up geo targeting database

Download free GeoCity Lite database from here. Unpack it somewhere on your local disk to get GeoLiteCity.dat file. Upload GeoLiteCity.dat file to your web server, for example in /App_Data/ directory.

2. Install Geotargeting Component

First download free Geotargeting .Net Component. Download Getoargeting .Net 1.1 for use with .Net Framework 1.1 web application, or Geotargeting .Net 2 for use with .Net Framework 2.0 or .Net Framework 3.5. Unzip .zip file to get component's dll. Upload dll to your web application's /bin/ folder.

3. Get Visitor City, Country and other information with ASP.NET code

Now everything is ready to use geo targeting on your web site. To get wanted information you can use a code like this:

[ C# ]

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        // Full path to GeoLiteCity.dat file
        string FullDBPath = Server.MapPath("App_Data/GeoLiteCity.dat");
        // Visitor's IP address
        string VisitorIP = Request.ServerVariables["REMOTE_ADDR"];
 
        // Create objects needed for geo targeting
        Geotargeting.LookupService ls = new Geotargeting.LookupService(FullDBPath,
    Geotargeting.LookupService.GEOIP_STANDARD);
        Geotargeting.Location visitorLocation = ls.getLocation(VisitorIP);
 
        // Get geo targeting data
        Response.Write("Your IP Address: " + VisitorIP + "<br />");
        Response.Write("Your country: " + visitorLocation.countryName + "<br />");
        Response.Write("Your country code: " + visitorLocation.countryCode + "<br />");
        Response.Write("Your region: " + visitorLocation.region + "<br />");
        Response.Write("Your city: " + visitorLocation.city + "<br />");
        Response.Write("Your postal code: " + visitorLocation.postalCode + "<br />");
        Response.Write("Area code: " + visitorLocation.area_code + "<br />");
        Response.Write("dma: " + visitorLocation.dma_code + "<br />");
        Response.Write("Latitude: " + visitorLocation.latitude + "<br />");
        Response.Write("Longitude: " + visitorLocation.longitude + "<br />");
    }
}

[ VB.NET ]

Partial Class Test_DefaultVB
    Inherits System.Web.UI.Page
 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        ' Full path to GeoLiteCity.dat file
        Dim FullDBPath As String = Server.MapPath("App_Data/GeoLiteCity.dat")
        ' Visitor's IP address
        Dim VisitorIP As String = Request.ServerVariables("REMOTE_ADDR")
 
        ' Create objects needed for geo targeting
        Dim ls As Geotargeting.LookupService = New  _
          Geotargeting.LookupService(FullDBPath, _
          Geotargeting.LookupService.GEOIP_STANDARD)
        Dim visitorLocation As Geotargeting.Location = ls.getLocation(VisitorIP)
 
        ' Get geo targeting data
        Response.Write("Your IP Address: " & VisitorIP & "<br />")
        Response.Write("Your country: " & visitorLocation.countryName & "<br />")
        Response.Write("Your country code: " & visitorLocation.countryCode & "<br />")
        Response.Write("Your region: " & visitorLocation.region & "<br />")
        Response.Write("Your city: " & visitorLocation.city & "<br />")
        Response.Write("Your postal code: " + visitorLocation.postalCode + "<br />")
        Response.Write("Area code: " & visitorLocation.area_code & "<br />")
        Response.Write("dma: " & visitorLocation.dma_code & "<br />")
        Response.Write("Latitude: " & visitorLocation.latitude & "<br />")
        Response.Write("Longitude: " & visitorLocation.longitude & "<br />")
    End Sub
End Class

 

That is all you need to do! Enjoy in new feature of your web site :)

Common errors during implementation

If you access to sample code through a local area network or through a http://localhost/ you'll get an error "Object reference not set to an instance of an object". Note that this technique doesn't return location information directly, but based on IP address. When you try to run example on http://localhost/ IP address is 127.0.0.1

If you set incorrect path to .dat file, you'll get the same error. In that case check value of FullDBPath variable in example code and check is GeoLiteCity.dat file existing in specified location.

Geo targeting maintenance

Data about IP addresses are not constant. They change over time and database becomes less accurate. Because of that, MaxMind refreshes its databases every month to include those changes and provide correct results. You need to download .dat file once per month and upload it to your web site over old file.

Comparation between free version and commercial version of database

Both databases works on completely same way. You can use Geotargeting component with both editions. However, commercial version have more accurate data. You can see all differences in table at the bottom of this page.


Tutorial toolbar:  Tell A Friend  |  Add to favorites  |  Feedback



Related articles:

1. Introduction To Assemblies
2. CAPTCHA - prevent bots and automatic submissions in ASP.NET