/// <summary> /// Gets a JSON list of the available continents /// </summary> /// <returns>JSON response of available criteria</returns> public JsonResult GetContinents() { var cacheKey = $"PersonalisationGroups_GeoLocation_Continents"; var countries = RuntimeCacheHelper.GetCacheItem(cacheKey, () => { var assembly = GetResourceAssembly(); var resourceName = GetResourceName("continents"); using (var stream = assembly.GetManifestResourceStream(resourceName)) { if (stream == null) { return(null); } using (var reader = new StreamReader(stream)) { var continentRecords = reader.ReadToEnd() .Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries) .Select(x => new { code = x.Split(',')[0], name = CleanName(x.Split(',')[1]) }); continentRecords = continentRecords.OrderBy(x => x.name); return(continentRecords); } } }); return(Json(countries, JsonRequestBehavior.AllowGet)); }
/// <summary> /// Gets a JSON list of the available regions for a given country /// </summary> /// <param name="countryCode">Country code</param> /// <returns>JSON response of available criteria</returns> public JsonResult GetRegions(string countryCode) { var cacheKey = $"PersonalisationGroups_GeoLocation_Regions_{countryCode}"; var regions = RuntimeCacheHelper.GetCacheItem(cacheKey, () => { using (var stream = GetStreamForRegions()) { if (stream == null) { return(null); } using (var reader = new StreamReader(stream)) { var streamContents = reader.ReadToEnd(); var regionRecords = streamContents .SplitByNewLine(StringSplitOptions.RemoveEmptyEntries) .Where(x => x.Split(',')[0] == countryCode.ToUpperInvariant()) .Select(x => new { code = x.Split(',')[1], name = CleanName(x.Split(',')[2]) }) .OrderBy(x => x.name); return(regionRecords); } } }); return(Json(regions, JsonRequestBehavior.AllowGet)); }
public Region GetRegionFromIp(string ip) { var cacheKey = $"PersonalisationGroups_GeoLocation_Region_{ip}"; var cachedItem = RuntimeCacheHelper.GetCacheItem(cacheKey, () => { try { using (var reader = new DatabaseReader(_pathToCityDb)) { try { var response = reader.City(ip); var region = new Region { City = response.City.Name, Subdivisions = response.Subdivisions .Select(x => x.Name) .Union(response.Subdivisions .SelectMany(x => x.Names .Where(y => !string.IsNullOrEmpty(y.Value)) .Select(y => y.Value))) .ToArray(), Country = new Country { Code = response.Country.IsoCode, Name = response.Country.Name, } }; return(region); } catch (AddressNotFoundException) { return(null); } catch (GeoIP2Exception ex) { if (IsInvalidIpException(ex)) { return(null); } throw; } } } catch (FileNotFoundException) { throw new FileNotFoundException( $"MaxMind Geolocation database required for locating visitor region from IP address not found, expected at: {_pathToCountryDb}. The path is derived from either the default ({AppConstants.DefaultGeoLocationCountryDatabasePath}) or can be configured using a relative path in an appSetting with key: \"{AppConstants.ConfigKeys.CustomGeoLocationCountryDatabasePath}\"", _pathToCountryDb); } }); return(cachedItem); }
public Country GetCountryFromIp(string ip) { var cacheKey = $"PersonalisationGroups_GeoLocation_Country_{ip}"; var cachedItem = RuntimeCacheHelper.GetCacheItem(cacheKey, () => { try { using (var reader = new DatabaseReader(_pathToCountryDb)) { try { var response = reader.Country(ip); return(new Country { Code = response.Country.IsoCode, Name = response.Country.Name, }); } catch (AddressNotFoundException) { return(null); } catch (GeoIP2Exception ex) { if (IsInvalidIpException(ex)) { return(null); } throw; } } } catch (FileNotFoundException) { throw new FileNotFoundException( $"MaxMind Geolocation database required for locating visitor country from IP address not found, expected at: {_pathToCountryDb}. The path is derived from either the default ({AppConstants.DefaultGeoLocationCountryDatabasePath}) or can be configured using a relative path in an appSetting with key: \"{AppConstants.ConfigKeys.CustomGeoLocationCountryDatabasePath}\"", _pathToCountryDb); } }); return(cachedItem); }
/// <summary> /// Gets a JSON list of the available countries /// </summary> /// <returns>JSON response of available criteria</returns> public JsonResult GetCountries(bool withRegionsOnly = false) { var cacheKey = $"PersonalisationGroups_GeoLocation_Countries_{withRegionsOnly}"; var countries = RuntimeCacheHelper.GetCacheItem(cacheKey, () => { var assembly = GetResourceAssembly(); var resourceName = GetResourceName("countries"); using (var stream = assembly.GetManifestResourceStream(resourceName)) { if (stream == null) { return(null); } using (var reader = new StreamReader(stream)) { var countryRecords = reader.ReadToEnd() .SplitByNewLine(StringSplitOptions.RemoveEmptyEntries) .Select(x => new { code = x.Split(',')[0], name = CleanName(x.Split(',')[1]) }); if (withRegionsOnly) { var countryCodesWithRegions = GetCountryCodesWithRegions(assembly); countryRecords = countryRecords .Where(x => countryCodesWithRegions.Contains(x.code)); } countryRecords = countryRecords.OrderBy(x => x.name); return(countryRecords); } } }); return(Json(countries, JsonRequestBehavior.AllowGet)); }