How to get geolocation information and redirect based on IP address
This article shows you how to get geolocation information of your web visitor and how to redirect to different page by using IP2Location Nuget package.
- Open your project solution in Microsoft .NET. Go to the “References” section on the solution explorer which is on the right hand side of your project.
- Select “Manage NuGet Packages” after you right-click to launch the reference option.
- Search for “ip2location” and select “IP2Location.IPGeolocation” in order to install the package. Please make sure the version you selected is the latest one – 7.0.0.
- Click OK to accept changes for your current project.
- You can notice that the IP2Location extension has been added into the references list.
- You can also check your “package.config” for the current installed IP2Location package.
- Next, extract your IP2Location database bin file from the IP2Location package folder in your current web project. The installed package folder only gives you the sample database. You can download the DB11 Lite database bin file from IP2Location LITE and put them on a specific folder as you will need them on the following sample C# code.
- Compile and run your project solution and you will get the result as below. User will be redirected to the Korean google website when a Korean IP address has detected.
Sample Code (C#)
using IP2Location; namespace IP2LocationWebsiteRedirect { public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } override protected void OnInit(EventArgs e) { InitializeComponent(); base.OnInit(e); } private void InitializeComponent() { this.btnQuery.Click += new System.EventHandler(this.btnQuery_Click); this.Load += new System.EventHandler(this.Page_Load); } private void btnQuery_Click(object sender, System.EventArgs e) { this.txtIPResult.Text = ""; this.Query(this.txtIPAddress.Text); } protected void OpenWindowJP() { string url = "https://www.google.co.jp/"; string s = "window.open('" + url + "', 'popup_window', 'width=800,height=500,left=100,top=100,resizable=yes');"; ClientScript.RegisterStartupScript(this.GetType(), "script", s, true); } protected void OpenWindowEN() { string url = "https://www.google.com/"; string s = "window.open('" + url + "', 'popup_window', 'width=800,height=500,left=100,top=100,resizable=yes');"; ClientScript.RegisterStartupScript(this.GetType(), "script", s, true); } protected void OpenWindowKR() { string url = "https://www.google.co.kr/"; string s = "window.open('" + url + "', 'popup_window', 'width=800,height=500,left=100,top=100,resizable=yes');"; ClientScript.RegisterStartupScript(this.GetType(), "script", s, true); } private void Query(string strIPAddress) { IP2Location.IPResult oIPResult = new IP2Location.IPResult(); IP2Location.Component oIP2Location = new IP2Location.Component(); try { if (strIPAddress != "") { oIP2Location.IPDatabasePath = "C:\\Program Files\\IP2Location\\IP2LOCATION-LITE-DB11.BIN"; oIPResult = oIP2Location.IPQuery(strIPAddress); switch (oIPResult.Status.ToString()) { case "OK": this.txtIPResult.Text += "IP Address: " + oIPResult.IPAddress + "\n\n"; this.txtIPResult.Text += "City: " + oIPResult.City + "\n\n"; this.txtIPResult.Text += "Country Code: " + oIPResult.CountryShort + "\n\n"; this.txtIPResult.Text += "Country Name: " + oIPResult.CountryLong + "\n\n"; this.txtIPResult.Text += "Postal Code: " + oIPResult.ZipCode + "\n\n"; this.txtIPResult.Text += "Domain Name: " + oIPResult.DomainName + "\n\n"; this.txtIPResult.Text += "ISP Name: " + oIPResult.InternetServiceProvider + "\n\n"; this.txtIPResult.Text += "Latitude: " + oIPResult.Latitude + "\n\n"; this.txtIPResult.Text += "Longitude: " + oIPResult.Longitude + "\n\n"; this.txtIPResult.Text += "Region: " + oIPResult.Region + "\n\n"; this.txtIPResult.Text += "Time Zone: " + oIPResult.TimeZone + "\n\n"; this.txtIPResult.Text += "Net Speed: " + oIPResult.NetSpeed + "\n\n"; this.txtIPResult.Text += "IDD Code: " + oIPResult.IDDCode + "\n\n"; this.txtIPResult.Text += "Area Code: " + oIPResult.AreaCode + "\n\n"; this.txtIPResult.Text += "Weather Station Code: " + oIPResult.WeatherStationCode + "\n\n"; this.txtIPResult.Text += "Weather Station Name: " + oIPResult.WeatherStationName + "\n\n"; this.txtIPResult.Text += "MCC: " + oIPResult.MCC + "\n\n"; this.txtIPResult.Text += "MNC: " + oIPResult.MNC + "\n\n"; this.txtIPResult.Text += "Mobile Brand: " + oIPResult.MobileBrand + "\n\n"; this.txtIPResult.Text += "Elevation: " + oIPResult.Elevation + "\n\n"; this.txtIPResult.Text += "Usage Type: " + oIPResult.UsageType + "\n\n"; if (oIPResult.CountryShort == "JP") { OpenWindowJP(); }else if (oIPResult.CountryShort == "KR") { OpenWindowKR(); } else { OpenWindowEN(); } break; case "EMPTY_IP_ADDRESS": Response.Write("IP Address cannot be blank."); break; case "INVALID_IP_ADDRESS": Response.Write("Invalid IP Address."); break; case "MISSING_FILE": Response.Write("Invalid Database Path."); break; } } else { Response.Write("IP Address cannot be blank."); } } catch (Exception ex) { Response.Write(ex.Message); } finally { oIPResult = null; oIP2Location = null; } } } }