Professional Applications Programmers/Consultants برمجة واستشارات تطبيقات الإنترنت
Skip Navigation LinksHome » Code Library » GeoIpController

Public general use code classes and xml files that we've compiled and used over the years:

GeoIp API Controller class of Internet Appplication project model.

   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.Linq;
   4:  using System.Net;
   5:  using System.Web.Http;
   6:  using System.Runtime.Serialization;
   7:   
   8:  namespace Ia.Cl.Model.Api.Controller
   9:  {
  10:      ////////////////////////////////////////////////////////////////////////////
  11:   
  12:      /// <summary publish="true">
  13:      /// GeoIp API Controller class of Internet Appplication project model.
  14:      /// </summary>
  15:      /// 
  16:      /// <remarks> 
  17:      /// Copyright © 2006-2018 Jasem Y. Al-Shamlan (info@ia.com.kw), Internet Applications - Kuwait. All Rights Reserved.
  18:      ///
  19:      /// This library is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
  20:      /// the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
  21:      ///
  22:      /// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  23:      /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  24:      /// 
  25:      /// You should have received a copy of the GNU General Public License along with this library. If not, see http://www.gnu.org/licenses.
  26:      /// 
  27:      /// Copyright notice: This notice may not be removed or altered from any source distribution.
  28:      /// </remarks> 
  29:      public class GeoIpController : ApiController
  30:      {
  31:          ////////////////////////////////////////////////////////////////////////////
  32:   
  33:          /// <summary/>
  34:          [DataContract]
  35:          public class CountryInfo
  36:          {
  37:              /// <summary/>
  38:              [DataMember]
  39:              public string Name { get; set; }
  40:   
  41:              /// <summary/>
  42:              [DataMember]
  43:              public string IsoCode { get; set; }
  44:          }
  45:   
  46:          ////////////////////////////////////////////////////////////////////////////
  47:   
  48:          /// <summary/>
  49:          [DataContract]
  50:          public class CityInfo
  51:          {
  52:              /// <summary/>
  53:              [DataMember]
  54:              public string Name { get; set; }
  55:   
  56:              /// <summary/>
  57:              [DataMember]
  58:              public double? Latitude { get; set; }
  59:   
  60:              /// <summary/>
  61:              [DataMember]
  62:              public double? Longitude { get; set; }
  63:          }
  64:   
  65:          ////////////////////////////////////////////////////////////////////////////
  66:   
  67:          /// <summary>
  68:          /// 
  69:          /// </summary>
  70:          public GeoIpController()
  71:          {
  72:          }
  73:   
  74:          ////////////////////////////////////////////////////////////////////////////
  75:   
  76:          /// <summary>
  77:          ///
  78:          /// </summary>
  79:          [HttpGet]
  80:          public CountryInfo Country(string ip)
  81:          {
  82:              CountryInfo countryInfo;
  83:   
  84:              countryInfo = new CountryInfo();
  85:   
  86:              if (Model.Default.IsValidIp(ip))
  87:              {
  88:                  using (MaxMind.GeoIP2.DatabaseReader reader = new MaxMind.GeoIP2.DatabaseReader(global::Ia.Cl.Model.Default.AbsolutePath() + "app_data\\geoip\\GeoLite2-Country.mmdb", MaxMind.Db.FileAccessMode.Memory))
  89:                  {
  90:                      MaxMind.GeoIP2.Responses.AbstractCountryResponse abstractCountryResponse;
  91:                      abstractCountryResponse = reader.Country(ip);
  92:   
  93:                      countryInfo.Name = abstractCountryResponse.Country.Name;
  94:                      countryInfo.IsoCode = abstractCountryResponse.Country.IsoCode;
  95:                  }
  96:              }
  97:              else countryInfo = null;
  98:   
  99:              return countryInfo;
 100:          }
 101:   
 102:          ////////////////////////////////////////////////////////////////////////////
 103:   
 104:          /// <summary>
 105:          ///
 106:          /// </summary>
 107:          [HttpGet]
 108:          public CityInfo CityAndLocation(string ip)
 109:          {
 110:              CityInfo cityInfo;
 111:   
 112:              cityInfo = new CityInfo();
 113:   
 114:              if (Model.Default.IsValidIp(ip))
 115:              {
 116:                  using (MaxMind.GeoIP2.DatabaseReader reader = new MaxMind.GeoIP2.DatabaseReader(global::Ia.Cl.Model.Default.AbsolutePath() + "app_data\\geoip\\GeoLite2-City.mmdb", MaxMind.Db.FileAccessMode.Memory))
 117:                  {
 118:                      MaxMind.GeoIP2.Responses.AbstractCityResponse abstractCityResponse;
 119:                      abstractCityResponse = reader.City(ip);
 120:   
 121:                      cityInfo.Name = abstractCityResponse.City.Name;
 122:                      cityInfo.Latitude = abstractCityResponse.Location.Latitude;
 123:                      cityInfo.Longitude = abstractCityResponse.Location.Longitude;
 124:                  }
 125:              }
 126:              else cityInfo = null;
 127:   
 128:              return cityInfo;
 129:          }
 130:   
 131:          ////////////////////////////////////////////////////////////////////////////
 132:          ////////////////////////////////////////////////////////////////////////////
 133:      }
 134:  }