ip2location facebbook  ip2location twitter  ip2location google+

Redirect Web Visitors By Country Using .NET Framework in Nuget

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.

  1. 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.
  2. Select “Manage NuGet Packages” after you right-click to launch the reference option.

  3. 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.

  4. Click OK to accept changes for your current project.

  5. You can notice that the IP2Location extension has been added into the references list.

  6. You can also check your “package.config” for the current installed IP2Location package.

  7. 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.
  8. 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;
            }
        }
    }
}

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.