There can be some requirements that redirect the page or may be block the access of some section or some part of website based on country. There can be some situation that you may need to change some contents based on country.

Requirement: 1. To find the country when website is opened or find the country from IP address.

Solution: This requirement can be catered in several ways.

You can add some code in application start to identify the IP and then country, city, region likewise details from the IP.

There are many API(s) that will give the details.

  1. IPSTACK: ipstack offers a powerful, real-time IP to geolocation API capable of looking up accurate location data and assessing security threats originating from risky IP addresses. Results are delivered within milliseconds in JSON or XML format. Using the ipstack API you will be able to locate website visitors at first glance and adjust your user experience and application accordingly.

IPSTACK Ap is very simple to use.
    ? access_key = YOUR_ACCESS_KEY

You can get free access_key by following above link.

API Response:

  "ip": "",
  "hostname": "",
  "type": "ipv4",
  "continent_code": "NA",
  "continent_name": "North America",
  "country_code": "US",
  "country_name": "United States",
  "region_code": "CA",
  "region_name": "California",
  "city": "Los Angeles",
  "zip": "90013",
  "latitude": 34.0453,
  "longitude": -118.2413,
  "location": {
    "geoname_id": 5368361,
    "capital": "Washington D.C.",
    "languages": [
          "code": "en",
          "name": "English",
          "native": "English"
    "country_flag": "",
    "country_flag_emoji": "🇺🇸",
    "country_flag_emoji_unicode": "U+1F1FA U+1F1F8",
    "calling_code": "1",
    "is_eu": false
  "time_zone": {
    "id": "America/Los_Angeles",
    "current_time": "2018-03-29T07:35:08-07:00",
    "gmt_offset": -25200,
    "code": "PDT",
    "is_daylight_saving": true
  "currency": {
    "code": "USD",
    "name": "US Dollar",
    "plural": "US dollars",
    "symbol": "$",
    "symbol_native": "$"
  "connection": {
    "asn": 25876,
    "isp": "Los Angeles Department of Water & Power"
  "security": {
    "is_proxy": false,
    "proxy_type": null,
    "is_crawler": false,
    "crawler_name": null,
    "crawler_type": null,
    "is_tor": false,
    "threat_level": "low",
    "threat_types": null

Solution 2: using api.

Using simple javascript:

$.get(“”, function (response) {
if( == “NP”)
//window.location.replace(“site for Nepal”);

Response in above js

    "ip": "",
    "city": "Paltan",
    "region": "Dhaka",
    "country": "BD",
    "loc": "23.7916,90.4152",
    "org": "AS23688 Link3 Technologies Ltd.",
    "postal": "1212",
    "timezone": "Asia/Dhaka",
    "readme": ""

Solution 3: C#

Get IP

//Get CountyLocation based on IP
public static string GetCountyLocation(string IP)
        string url = "" + IP + "?access_key=[KEY]";
        var request = System.Net.WebRequest.Create(url);

         using (WebResponse wrs = request.GetResponse())
         using (Stream stream = wrs.GetResponseStream())
         using (StreamReader reader = new StreamReader(stream))
          string json = reader.ReadToEnd();
          var obj = JObject.Parse(json);
            string City = (string)obj["city"];
            string Country = (string)obj["region_name"];                    
            string CountryCode = (string)obj["country_code"];

           return (CountryCode + " - " + Country +"," + City);

  return "";

//To Get IP
protected string GetIPAddress()
    System.Web.HttpContext context = System.Web.HttpContext.Current; 
    string ipAddress = context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

    if (!string.IsNullOrEmpty(ipAddress))
        string[] addresses = ipAddress.Split(',');
        if (addresses.Length != 0)
            return addresses[0];

    return context.Request.ServerVariables["REMOTE_ADDR"];

Kind Regards

By Rijwan Ansari

Research and Technology Lead | Software Architect | Full Stack .NET Expert | Tech Blogger | Community Speaker | Trainer | YouTuber. Follow me @ Md Rijwan Ansari is a high performing and technology consultant with 10 plus years of Software Development and Business Applications implementation using .NET Technologies, SharePoint, Power Platform, Data, AI, Azure and cognitive services. He is also a Microsoft Certified Trainer, C# Corner MVP, Microsoft Certified Data Analyst Associate, Microsoft Certified Azure Data Scientist Associate, CSM, CSPO, MCTS, MCP, with 15+ Microsoft Certifications. He is a research and technology lead in Tech One Global as well as leading Facebook community Cloud Experts Group and SharePoint User Group Nepal. He is a active contributor and speaker in community, C# Corner MVP and his rank at 20 among 3+ millions members. Additionally, he is knee to learn new technologies, write articles, love to contribute to the open-source community. Visit his blog RIJSAT.COM for extensive articles, courses, news, videos and issues resolution specially for developer and data engineer.

Leave a Reply

Your email address will not be published. Required fields are marked *