asp.net, IP get county

I have asp.net/c# web application.
On  the masterpage, I want to do the below.

Detect the end user's IP address
Determine the country based on the end user.
Send message to the end user to choose his/her language based on the country.

Is it possible? Can you show me how can I do that?
LVL 1
ITsolutionWizardAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
Hi ITsolutionWizard;

First to let you know in advance I am not a Web developer and so I am not sure that this is the most efficient way to code this. The below code snippet should work from your master page. Although it may not work from a development machine using the development web server because it is using localhost as the IP Address. This code also uses Newtonsoft.Json API and can be gotten from NuGet package manager.

using Newtonsoft.Json;

protected void Page_Load(object sender, EventArgs e)
{
    if(!Page.IsPostBack)
    {
        // Get Request object
        HttpRequest request = base.Request;
        // Get Responce object
        HttpResponse responce = base.Response;

        // Get UserHostAddress property.
        string address = request.UserHostAddress;

        var data = GetIPData<IPData>("http://ipinfo.io/" + address + "/json");

        if (data.ip != null)
        {
            string info = String.Format("IP : {0}   County : {1}", data.ip, data.country);
            responce.Write(info);
        }
        else
            responce.Write("No information found");

    }
}

private static T GetIPData<T>(string url) where T : new()
{
    using (var webData = new WebClient())
    {
        var jsonData = string.Empty;

        try
        {
            jsonData = webData.DownloadString(url);
        }
        catch (Exception ex)
        {
            //(ex.Message);
            return new T();
        }
        return !string.IsNullOrEmpty(jsonData)
                    ? JsonConvert.DeserializeObject<T>(jsonData)
                    : new T();
    }
}

// Used to deserialize Json object
public class IPData 
{
    public string ip { get; set; }
    public string hostname { get; set; }
    public string loc { get; set; }
    public string org { get; set; }
    public string city { get; set; }
    public string region { get; set; }
    public string country { get; set; }
    public int phone { get; set; }
}

Open in new window

Fernando SotoRetiredCommented:
Here is the developer web page for the API used in the above code.
ITsolutionWizardAuthor Commented:
1. How can I get the country only.
2. Mine returns :::0. and when I go to whatismyip.com I got 206.xx.xx.xxx. And I want to get 206.xx.xx.xxx.  And I used your codes above so it sounds like not working.
Fernando SotoRetiredCommented:
To your statement, "How can I get the country only", change this line of code, "string info = String.Format("IP : {0}   County : {1}", data.ip, data.country);", to this, "string info =  data.country;".

 To your statement, "Mine returns :::0. and when I go to whatismyip.com I got 206.xx.xx.xxx.", If you implemented my code as I posted it I can only assume that you are running this off of your development machine and using the development web server. I stated in my original post to this as follows, "Although it may not work from a development machine using the development web server because it is using localhost as the IP Address", therefor you need to run the Web App on a system that will return an actual IP address and not the word "localhost" which is a mapping to the local machine.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Fernando SotoRetiredCommented:
Was there something that was not correct that was not worthy of an A?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.