I'm writing a web app in asp.net core mvc and I need to find current location- at least on city level how do I do this? is there a Nuget package I could install or something? because the .net library that supports this is only in .net framework
CodePudding user response:
You could use a service like ip-api.com.
here is the documentation for retrieving user location data by ip adress where with a json response: https://ip-api.com/docs/api:json
simply get the users ip adress in the controller like shown here and call their api with the users ip. ex. http://ip-api.com/json/24.48.0.1
CodePudding user response:
I need to find current location at least on city level how do I do this?
Assuming you have the IP address of the server or the client, you can get the city level location information using IPinfo.
is there a Nuget package I could install or something?
Check out IPinfo C# .Net SDK. You can install it via NuGet
:
nuget install IPinfo
To get started:
// namespace
using IPinfo;
using IPinfo.Models;
// initializing IPinfo client
string token = "MY_TOKEN";
IPinfoClient client = new IPinfoClient.Builder()
.AccessToken(token)
.Build();
// making API call
string ip = "8.8.8.8";
IPResponse ipResponse = await client.IPApi.GetDetailsAsync(ip);
// then access the city level location
Console.WriteLine($"IPResponse.City: {ipResponse.City}");
Disclaimer: I work for IPinfo.