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 from NuGet.

Download at https://www.nuget.org/packages/IP2Location.IPGeolocation/ and install in your Visual Studio.

Next, you will need to download the IP2Location BIN file.

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 DB26 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 the BIN database path.
  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-ADDRESSTYPE-CATEGORY-DISTRICT-ASN.BIN" ' edit to your BIN file name

        myIP2Loc.MapFileName = "MyBIN" & System.Web.Security.Membership.GeneratePassword(8, 0).ToString
        myIP2Loc.Open(Server.MapPath(mydirectory & mybin), True) ' True for UseMemoryMappedFile
    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 += "Address Type: " & myresult.AddressType & vbNewLine
        mystr += "Category: " & myresult.Category & vbNewLine
        mystr += "District: " & myresult.District & vbNewLine
        mystr += "ASN: " & myresult.ASN & vbNewLine
        mystr += "AS: " & myresult.AS & vbNewLine
        mystr += "Status: " & myresult.Status & vbNewLine
 
        Response.Write(mystr)
    End Sub
 
End Class

Was this article helpful?

Related Articles