C# – Convert IP Address to Location
This tutorial not only teaches you how to find the Location of an IP Address. It also teaches you the following things,
1) How to use Rest Web Service?
2) How to parse JSON Data?
Source Code: https://github.com/SaiBalaji202/IP-Address-to-Location.git
ip address
Is this code run to fetch mobile ip address means mobile location ipv4
Sir can i achieve this functionality in .net mvc same code
Very nice, I've adapt it slightly for anyone who needs it for ASP.NET core 2.2 or above.
I've added a model to map the json data so that I can extract specific information such as city or region:
First, right click on your model folder and create a class call it anything I called mine Location.cs with the following attributes:
public class Location
{
public async Task<string> GetGeoInfo()
{
var client = new RestClient("https://ipapi.co/json");
var request = new RestRequest()
{
Method = Method.GET
};
var response = client.Execute(request);
var json = response.Content.ToString();
return json;
}
}
Next, create a JsonToViewModel.cs which we will use to map the json values..In the JsonToViewModel I have the following properties:
public class JsonToViewModel
{
[JsonProperty("ip")]
public string IP { get; set; }
[JsonProperty("city")]
public string City { get; set; }
[JsonProperty("region")]
public string Region { get; set; }
[JsonProperty("region_code")]
public string RegionCode { get; set; }
[JsonProperty("country_name")]
public string CountryName { get; set; }
[JsonProperty("country_code")]
public string CountryCode { get; set; }
}
Now, in the controller class you can either write a function or call it in a Index action method (or whatever action you prefer), for this I created a function in the HomeController called, GeoLocation like this:
public async Task<IActionResult> GeoLocation()
{
Location geoHelper = new Location(); //the model class
JsonToViewModel model = new JsonToViewModel(); //jsonVM class to mapp
var result = await geoHelper.GetGeoInfo(); //fetch ip information from the location model
model = JsonConvert.DeserializeObject<JsonToViewModel>(result); //convert and map the return json data to the jsontoviewmodel
//Now doing any of this will give you access to a particular IP info as specified in your Location Model.
//var Ip = model.IP;
//var countryName = model.CountryName;
//var city = model.City;
//var region = model.Region;
return View(model);
}
In the Index Action you can call GeoLocation();
This is very useful if you need to get just one or two info from the IP request so thought I'd share. Hope this helps.
Very helpful dear 😍
Hey can you tell me what are the other resources that we need to run this application
Good project bro go fot it yu can make console application version?
Subtitles?
I can't understand
help me
i have problem = foreach (object key in dictionary.Keys) " System.NullReferenceException: 'Object reference not set to an instance of an object.'"
awesome videos thanx buddy now i have mine which i learnt from this tutorial
Awesome tutorial!
It also helped me to webrequest from api yesterday! 😄
Also you can use new RestRequest(Method.GET);
Thank you Balaji. Nice explanation and awesome video
Great video.
thanks a lot , please how can I get only the latitude and longitude to look upon it in a map?