In this article I will explain with an example, how to get the visitor’s Geographic Location (GeoLocation) details like Country, City, Region, Zip Code, Latitude, Longitude and Time zone using the Client IP Address of his machine in ASP.Net MVC Razor.
The Visitor’s Geographic Location (GeoLocation) will be determined with the help of FreeGeoIP API which is IP to Location API and returns the visitor’s Geographic Location (GeoLocation) details like Country, City, Region, Zip Code, Latitude, Longitude and Time zone using the Client IP Address of his machine in ASP.Net MVC Razor.
 
 
Model
The model class consists of the following properties.
public class LocationModel
{
    public string IP { get; set; }
    public string Country_Code { get; set; }
    public string Country_Name { get; set; }
    public string Region_Code { get; set; }
    public string Region_Name { get; set; }
    public string City { get; set; }
    public string Zip_Code { get; set; }
    public string Time_Zone { get; set; }
    public string Latitude { get; set; }
    public string Longitude { get; set; }
    public string Metro_Code { get; set; }
}
 
 
Namespaces
You will need to import the following namespaces.
using System.Net;
using System.Web.Script.Serialization;
 
 
Controller
Inside the Index Action method of the controller, first the IP Address is determined.
Note: For more details on fetching IP Address, please refer my article How to get Client IP Address of Visitors Machine in ASP.Net MVC.
 
The IP Address is then sent as parameter to the FreeGeoIP API which is being called using the WebClient class object.
The returned JSON is deserialized into the LocationModel class object which is finally returned to the View.
public class HomeController : Controller
{
    // GET: Home
    public ActionResult Index()
    {
        string ipAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
        if (string.IsNullOrEmpty(ipAddress))
        {
            ipAddress = Request.ServerVariables["REMOTE_ADDR"];
        }
        LocationModel location = new LocationModel();
        string url = string.Format("http://freegeoip.net/json/{0}", ipAddress);
        using (WebClient client = new WebClient())
        {
            string json = client.DownloadString(url);
            location = new JavaScriptSerializer().Deserialize<LocationModel>(json);
        }
 
        return View(location);
    }
}
 
 
View
Inside the View, the LocationModel class is declared as the Model for the View.
The fetched visitor’s Geographic Location (GeoLocation) details like Country, City, Region, Zip Code, Latitude, Longitude and Time zone are displayed in the View.
@model IPAddress_Location_MVC.Models.LocationModel
 
@{
    Layout = null;
}
 
<!DOCTYPE html>
 
<html>
<head>
    <meta name="viewport" content="width=device-width"/>
    <title>Index</title>
</head>
<body>
    <table cellpadding="0" cellspacing="0">
        <tr><td>IP</td><td>@Model.IP</td></tr>
        <tr><td>Country Code</td><td>@Model.Country_Code</td></tr>
        <tr><td>Country Name</td><td>@Model.Country_Name</td></tr>
        <tr><td>Region Code</td><td>@Model.Region_Code</td></tr>
        <tr><td>Region Name</td><td>@Model.Region_Name</td></tr>
        <tr><td>City</td><td>@Model.City</td></tr>
        <tr><td>Zip Code</td><td>@Model.Zip_Code</td></tr>
        <tr><td>Time Zone</td><td>@Model.Time_Zone</td></tr>
        <tr><td>Latitude</td><td>@Model.Latitude</td></tr>
        <tr><td>Longitude</td><td>@Model.Longitude</td></tr>
        <tr><td>Metro Code</td><td>@Model.Metro_Code</td></tr>
    </table>
</body>
</html>
 
 
Screenshot
Find Visitors Geographic Location (GeoLocation) using IP Address in ASP.Net MVC
 
 
Downloads