)>}]
شركة التطبيقات المتكاملة لتصميم وبرمجة البرمجيات الخاصة ش.ش.و.
Integrated Applications Programming Company
Home » Code Library » GeoIpController (Ia.Api.Wa.Controllers)

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

GeoIp API Controller class of Internet Application project model.

    1: using MaxMind.GeoIP2.Responses;
    2: using Microsoft.AspNetCore.Mvc;
    3:  
    4: namespace Ia.Api.Wa.Controllers
    5: {
    6:     ////////////////////////////////////////////////////////////////////////////
    7:  
    8:     /// <summary publish="true">
    9:     /// GeoIp API Controller class of Internet Application project model.
   10:     /// </summary>
   11:     /// 
   12:     /// <remarks> 
   13:     /// Copyright © 2006-2024 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:     [ApiController]
   26:     [Route("api/geoip")]
   27:     public class GeoIpController : ControllerBase
   28:     {
   29:         private readonly string database = Directory.GetCurrentDirectory() + @"\wwwroot\app_data\dp-ip\dbip-country-lite-2024-08.mmdb";
   30:  
   31:         ////////////////////////////////////////////////////////////////////////////
   32:  
   33:         /// <summary>
   34:         ///
   35:         /// </summary>
   36:         [HttpGet]
   37:         [Route("countries/{ip}")]
   38:         public Ia.Cl.Models.GeoIp.CountryInfo? Country(string ip)
   39:         {
   40:             Ia.Cl.Models.GeoIp.CountryInfo? countryInfo;
   41:  
   42:             if (Ia.Cl.Models.Default.IsValidIpv4(ip))
   43:             {
   44:                 using (MaxMind.GeoIP2.DatabaseReader reader = new MaxMind.GeoIP2.DatabaseReader(database, MaxMind.Db.FileAccessMode.Memory))
   45:                 {
   46:                     if (reader.TryCountry(ip, out MaxMind.GeoIP2.Responses.CountryResponse? countryResponse))
   47:                     {
   48:                         countryInfo = new Ia.Cl.Models.GeoIp.CountryInfo();
   49:  
   50:                         countryInfo.Name = countryResponse.Country.Name;
   51:                         countryInfo.IsoCode = countryResponse.Country.IsoCode;
   52:                     }
   53:                     else countryInfo = default;
   54:                 }
   55:             }
   56:             else countryInfo = default;
   57:  
   58:             return countryInfo;
   59:         }
   60:  
   61:         ////////////////////////////////////////////////////////////////////////////
   62:  
   63:         /// <summary>
   64:         ///
   65:         /// </summary>
   66:         [HttpGet]
   67:         [Route("countries/locations/{ip}")]
   68:         public Ia.Cl.Models.GeoIp.CityInfo? CityAndLocation(string ip)
   69:         {
   70:             Ia.Cl.Models.GeoIp.CityInfo? cityInfo;
   71:  
   72:             if (Ia.Cl.Models.Default.IsValidIpv4(ip))
   73:             {
   74:                 using (MaxMind.GeoIP2.DatabaseReader reader = new MaxMind.GeoIP2.DatabaseReader(database, MaxMind.Db.FileAccessMode.Memory))
   75:                 {
   76:                     if (reader.TryCity(ip, out MaxMind.GeoIP2.Responses.CityResponse? cityResponse))
   77:                     {
   78:                         cityInfo = new Ia.Cl.Models.GeoIp.CityInfo();
   79:  
   80:                         cityInfo.Name = cityResponse.City.Name;
   81:                         cityInfo.Latitude = cityResponse.Location.Latitude;
   82:                         cityInfo.Longitude = cityResponse.Location.Longitude;
   83:                     }
   84:                     else cityInfo = default;
   85:                 }
   86:             }
   87:             else cityInfo = default;
   88:  
   89:             return cityInfo;
   90:         }
   91:  
   92:         ////////////////////////////////////////////////////////////////////////////
   93:         ////////////////////////////////////////////////////////////////////////////
   94:     }
   95:  
   96:     ////////////////////////////////////////////////////////////////////////////
   97:     ////////////////////////////////////////////////////////////////////////////
   98: }