شركة التطبيقات المتكاملة لتصميم النظم البرمجية الخاصة

Integrated Applications Programming Company

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