0

I have a bootstrap enabled website two divs DivOne and DivTwo.

I want to display DivTwo if the country is India. I want to display DivOne if the country is anything other than India.

How can I implement this in ASP.NET/HTML/JavaScript?

This is what I have so far in C#:

public string LocalIPAddress()
 {
   IPHostEntry host;
   string localIP = "";
   host = Dns.GetHostEntry(Dns.GetHostName());
   foreach (IPAddress ip in host.AddressList)
   {
     if (ip.AddressFamily == AddressFamily.InterNetwork)
     {
       localIP = ip.ToString();
       break;
     }
   }
   return localIP;
 }

I also want the same code to run in mobile sites as well.

One small addition. I would rather prefer an online service that takes an IP address and gives the country name rather than installing a database on my server. Thanks!

Sam
  • 2,917
  • 1
  • 15
  • 28
CuriousDev
  • 1,255
  • 1
  • 21
  • 44
  • 2
    Can't answer your overall question, but here are a few services for the IP check: http://ipinfodb.com/ip_location_api.php http://www.geobytes.com/IpLocator.htm http://freegeoip.net/ http://www.hostip.info/use.html – HitMeWithYourBestShot Nov 01 '14 at 01:03
  • There are free databases to load, but most services appear to require a monthly or yearly free if you plan to call them as a service. – Jason W Nov 01 '14 at 01:19
  • I am ready to pay for a service. I want instant access if you know of any. THanks. – CuriousDev Nov 01 '14 at 01:27
  • @AndrewHotovy Thanks I will check them out! – CuriousDev Nov 01 '14 at 01:31

2 Answers2

1

This URL displays the ip, and country in a JSON format.

http://api.hostip.info/get_json.php

jkd
  • 1,045
  • 1
  • 11
  • 27
0

I'll give you few tips to solve your problem rather than giving you the complete solution :-)

First of all your function LocalIPAddress() does not return the public IP if you are connected to the internet via a Router. Try to modify it to get the correct IP. There are many ways to do that but here's a good discussion at SO for you to get an idea.

Secondly, there are many free web services that you could get the country details based on you external IP. Here's a service that returns country details as JSON, XML or in CSV format.

http://freegeoip.net/

Then, read this JSON (or any other format you prefer) information and find out the country name.

Now, it's very easy to display your preferred DIV based on the country name. Here's how you'd do it.

Your markup should look like this. Note that these divs have the attribute runat="server". What this means is that you've converted these HTML element to a server control so that you could access them in your code behind using their ids.

<div id="DivOne" visible="false" runat="server">
        <p>This is DIV 1</p>
    </div>

    <div id="DivTwo" visible="false" runat="server">
        <p>This is DIV 2</p>
    </div>

Now in your code behind do this (I assume that you've written the GetCountryName() based on above instructions)

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (GetCountryName().ToLower() == "india")
            {
                DivTwo.Visible = true;
            }
            else
            {
                DivOne.Visible = true;
            }
        }
    }

Hope this helps.

Community
  • 1
  • 1
Sam
  • 2,917
  • 1
  • 15
  • 28