Guest User

Untitled

a guest
Jan 5th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.58 KB | None | 0 0
  1. public class HomeController : Controller
  2. {
  3. public async Task<IActionResult> Index(string ip)
  4. {
  5. if (string.IsNullOrEmpty(ip))
  6. {
  7. ip = Request.HttpContext.Connection.RemoteIpAddress.MapToIPv4().ToString();
  8.  
  9. }
  10. Datos model = await Datos.QueryGeographicalLocationAsync(ip);
  11.  
  12. return View(model);
  13. }
  14.  
  15.  
  16. }
  17.  
  18. public class Datos
  19. {
  20. [JsonProperty("ip")]
  21. public string IP { get; set; }
  22.  
  23. [JsonProperty("country_code")]
  24.  
  25. public string CountryCode { get; set; }
  26.  
  27. [JsonProperty("country_name")]
  28.  
  29. public string CountryName { get; set; }
  30.  
  31. [JsonProperty("region_code")]
  32.  
  33. public string RegionCode { get; set; }
  34.  
  35. [JsonProperty("region_name")]
  36.  
  37. public string RegionName { get; set; }
  38.  
  39. [JsonProperty("city")]
  40.  
  41. public string City { get; set; }
  42.  
  43. [JsonProperty("zip_code")]
  44.  
  45. public string ZipCode { get; set; }
  46.  
  47. [JsonProperty("time_zone")]
  48.  
  49. public string TimeZone { get; set; }
  50.  
  51. [JsonProperty("latitude")]
  52.  
  53. public float Latitude { get; set; }
  54.  
  55. [JsonProperty("longitude")]
  56.  
  57. public float Longitude { get; set; }
  58.  
  59. [JsonProperty("metro_code")]
  60.  
  61. public int MetroCode { get; set; }
  62.  
  63. private Datos() { }
  64.  
  65. public static async Task<Datos> QueryGeographicalLocationAsync(string ipAddress)
  66. {
  67. HttpClient client = new HttpClient();
  68. string result = await client.GetStringAsync("http://freegeoip.net/json/" + ipAddress);
  69.  
  70. return JsonConvert.DeserializeObject<Datos>(result);
  71. }
  72. }
  73.  
  74. @model WebAPI.Models.Datos
  75.  
  76. @{
  77. ViewBag.Title = "Home Page";
  78. }
  79.  
  80. <form method="get" action=".">
  81. type simulate
  82. <input type="text" name="ip" value="@Model.IP" />
  83. <button>submit</button>
  84. </form>
  85.  
  86. <!--mce:0-->
  87. <table>
  88. <tr>
  89. <td>IP</td>
  90. <td>@Model.IP</td>
  91. </tr>
  92. <tr>
  93. <td>Country code</td>
  94. <td>@Model.CountryCode</td>
  95. </tr>
  96. <tr>
  97. <td>Country name</td>
  98. <td>@Model.CountryName</td>
  99. </tr>
  100. <tr>
  101. <td>Region code</td>
  102. <td>@Model.RegionCode</td>
  103. </tr>
  104. <tr>
  105. <td>Region name</td>
  106. <td>@Model.RegionName</td>
  107. </tr>
  108. <tr>
  109. <td>City</td>
  110. <td>@Model.City</td>
  111. </tr>
  112. <tr>
  113. <td>Zip code</td>
  114. <td>@Model.ZipCode</td>
  115. </tr>
  116. <tr>
  117. <td>Time zone</td>
  118. <td>@Model.TimeZone</td>
  119. </tr>
  120. <tr>
  121. <td>Latitude</td>
  122. <td>@Model.Latitude</td>
  123. </tr>
  124. <tr>
  125. <td>Longitude</td>
  126. <td>@Model.Longitude</td>
  127. </tr>
  128. <tr>
  129. <td>Metro code</td>
  130. <td>@Model.MetroCode</td>
  131. </tr>
  132. </table>
Add Comment
Please, Sign In to add comment