How to convert ip address to geolocation using asp net or razor systax or bing m

IP to Geolocation ASP.NET

Converting an IP address to geolocation can be a useful feature in many web applications. It allows you to determine the approximate physical location of a user on their IP address. In this article, we will explore how to achieve this using ASP.NET.

Step 1: Obtaining the IP Address

The first step is to the IP address of the user. In ASP.NET, you can retrieve the IP address using the Request.UserHostAddress property. Here's an example:

string ipAddress = Request.UserHostAddress;

Step 2: Sending a Request to Geolocation API

Once you have the IP address, the next step is to send a request to a geolocation API. There are several APIs that geolocation data based on IP addresses, such as MaxMind, GeoIP, or Bing Maps API. In this example, we will use the Bing Maps API.

To send a request to the Bing Maps API, you need to make an HTTP request to their endpoint, passing the IP address as a parameter. Here's an example:

string apiUrl = "" + ipAddress + "?key=YOUR_API_KEY";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(apiUrl);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

Note that you need to replace YOUR_API_KEY with your actual Bing Maps API key.

Step 3: Parsing the Response

Once you receive the response from the geolocation API, you need to parse the JSON or XML data to extract the relevant information. The structure of the response may vary depending on the API you are using.

For example, if you are using the Bing Maps API, you can parse the JSON response as follows:

using (StreamReader streamReader = new StreamReader(response.GetResponseStream()))
    string responseJson = streamReader.ReadToEnd();
    dynamic jsonResponse = JsonConvert.DeserializeObject(responseJson);
    string country = jsonResponse.resourceSets[0].[0].address.countryRegion;
    string city = jsonResponse.resourceSets[0].resources[0].address.locality;
    string latitude = jsonResponse.resourceSets[0].resources[0].point.coordinates[0];
    string longitude = jsonResponse.resourceSets[0].resources[0].point.coordinates[1];

In this example, we are extracting the country, city, latitude, and longitude from the JSON response.

Step 4: Displaying the Geolocation Information

Finally, you can display the geolocation information on your web page. You can use the extracted data to show the user's location on a map or provide relevant information.

For example, you can create HTML elements to display the country, city, latitude, and longitude:





Then, using JavaScript, you can populate these elements with the extracted geolocation data:

document.getElementById("country").innerHTML = country;
document.getElementById("city").innerHTML = city;
document.getElementById("latitude").innerHTML = latitude;
document.getElementById("longitude").innerHTML = longitude;


Converting an IP address to geolocation using ASP.NET is a powerful feature that can enhance the of your web applications. By following the steps outlined in this article, you can easily retrieve the user's IP address, send a request to a geolocation API, parse the response, and display the geolocation information on your web page.

Remember to replace YOUR_API_KEY with your actual Bing Maps API key and adjust the parsing logic based on the structure of the API response you are using.

Rate this post

Leave a Reply

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

Table of Contents