Deploying IP2Location .NET component as a global object in ASP.NET using VB.NET

The aim of this guide is to demonstrate the deployment of the IP2Location .NET component as a global object in ASP.NET.

First of all, you will need to download the IP2Location .NET component.
Download at http://ip2location.com/software/dot-net-component

Extract out the IP2Location.dll and copy to the bin folder of your ASP.NET website.

Next, you will need to download the IP2Location BIN file.
Download at http://ip2location.com/download

Extract out the BIN file and copy to the bin folder of your ASP.NET website.

Creating & instantiating the IP2Location .NET component as a global object

Copy & paste the following codes into your Global.asax.vb file. Our example uses the DB24 BIN file.

The code does the following:

  1. Declare a static IP2Location.Component variable inside the Global_asax class and call it myIP2Loc.
  2. Instantiate the myIP2Loc variable under Application_Start event.
  3. Assign both the BIN database & license paths.
  4. Set the UseMemoryMappedFile property to True. This will load the BIN file into a memory mapped file so there will be less file reads.

NOTE: There is no need to explicitly destroy the variable since it will be alive for the duration of the Application state.

Imports System.Web.SessionState

Public Class Global_asax
    Inherits System.Web.HttpApplication

    Public Shared myIP2Loc = New IP2Location.Component

    Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
        ' Fires when the application is started
        Dim mydirectory As String = "/bin/" ' edit to the path for your BIN file
        Dim mybin As String = "IP-COUNTRY-REGION-CITY-LATITUDE-LONGITUDE-ZIPCODE-TIMEZONE-ISP-DOMAIN-NETSPEED-AREACODE-WEATHER-MOBILE-ELEVATION-USAGETYPE.BIN" ' edit to your BIN file name
        myIP2Loc.IPDatabasePath = Server.MapPath(mydirectory & mybin)
        myIP2Loc.IPLicensePath = Server.MapPath(mydirectory & "license.key")
        myIP2Loc.UseMemoryMappedFile = True
        myIP2Loc.MapFileName = "MyBIN" & System.Web.Security.Membership.GeneratePassword(8, 0).ToString
    End Sub

    Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
        ' Fires when the session is started
    End Sub

    Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
        ' Fires at the beginning of each request
    End Sub

    Sub Application_AuthenticateRequest(ByVal sender As Object, ByVal e As EventArgs)
        ' Fires upon attempting to authenticate the use
    End Sub

    Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
        ' Fires when an error occurs
    End Sub

    Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
        ' Fires when the session ends
    End Sub

    Sub Application_End(ByVal sender As Object, ByVal e As EventArgs)
        ' Fires when the application ends
    End Sub

End Class

Calling the global object in your webpage

Copy & paste the following codes into your aspx.vb file. Our example is called Default.aspx.vb file.

The code will call the global IP2Location object and output the results. There is no need to create a new IP2Location object for each query.

Public Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim myresult As IP2Location.IPResult

        myresult = Global_asax.myIP2Loc.IPQuery("8.8.8.8")
        Dim mystr As String = ""
        mystr += "IP Address: " & myresult.IPAddress & vbNewLine
        mystr += "City: " & myresult.City & vbNewLine
        mystr += "Country Code: " & myresult.CountryShort & vbNewLine
        mystr += "Country Name: " & myresult.CountryLong & vbNewLine
        mystr += "Postal Code: " & myresult.ZipCode & vbNewLine
        mystr += "Domain Name: " & myresult.DomainName & vbNewLine
        mystr += "ISP Name: " & myresult.InternetServiceProvider & vbNewLine
        mystr += "Latitude: " & myresult.Latitude & vbNewLine
        mystr += "Longitude: " & myresult.Longitude & vbNewLine
        mystr += "Region: " & myresult.Region & vbNewLine
        mystr += "TimeZone: " & myresult.TimeZone & vbNewLine
        mystr += "NetSpeed: " & myresult.NetSpeed & vbNewLine
        mystr += "IDD Code: " & myresult.IDDCode & vbNewLine
        mystr += "Area Code: " & myresult.AreaCode & vbNewLine
        mystr += "Weather Station Code: " & myresult.WeatherStationCode & vbNewLine
        mystr += "Weather Station Name: " & myresult.WeatherStationName & vbNewLine
        mystr += "MCC: " & myresult.MCC & vbNewLine
        mystr += "MNC: " & myresult.MNC & vbNewLine
        mystr += "Mobile Brand: " & myresult.MobileBrand & vbNewLine
        mystr += "Elevation: " & myresult.Elevation & vbNewLine
        mystr += "Usage Type: " & myresult.UsageType & vbNewLine
        mystr += "Status: " & myresult.Status & vbNewLine

        Response.Write(mystr)
    End Sub

End Class

Related Products

IP2Location .NET Component
IP2Location .NET Component


Do you like this article? Share it with others by clicking the social media buttons below. We will write more articles related to this topic.