Description:
Controller:
In this example we explain that how
to find the visitors Geographic Location using IP Address in asp.net MVC.or how
to Get the Visitors Geographic Location using client IP Address in MVC. Or how
to find the Geo location of the Visitors using IP Address in MVC.
Here we use the FreeGeoIP API to
get or Find the Geo Location of the Visitors in Asp.Net MVC.
public class EmployeeController
: Controller
{
public ActionResult Index()
{
string
ClientipAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if
(string.IsNullOrEmpty(ClientipAddress))
{
ClientipAddress =
Request.ServerVariables["REMOTE_ADDR"];
}
LocationModel
location = new LocationModel();
string
url = string.Format("http://freegeoip.net/json/{0}",
ClientipAddress);
using
(WebClient client = new
WebClient())
{
string
json = client.DownloadString(url);
location = new JavaScriptSerializer().Deserialize<LocationModel>(json);
}
return
View(location);
}
}
View:
@model
IPAddress_Location_MVC.Models.LocationModel
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<title>Find Visitors Geographic Location (GeoLocation)
using IP Address in ASP.Net MVC</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>
0 comments:
Post a Comment