一、通过NuGet安装IP2Region(支持Framework)如果是Net6则可以使用IP2Region.Net版本
1、IP2Region使用方式:
using (DbSearcher search = new DbSearcher(CommonHelper.MapPath("/Resource/ip2region.db"))) { IP2Region.Models.DataBlock ipModel = search.MemorySearch(ip); string ipRegion = ipModel.Region; //ipModel.Region = "国家|区域|省份|城市|ISP" if (!string.IsNullOrEmpty(ipRegion)) res= ipRegion; }
2、IP2Region.Net使用方式:
ISearcher searcher = new Searcher(new CachePolicy(), Environment.CurrentDirectory + @"\Resource\ip2region.xdb"); string ipRegion = searcher.Search(ip) ?? string.Empty;
//注入ip2region所需服务
//Program.cs services.AddSingleton<ISearcher>(new Searcher(new CachePolicy(), Environment.CurrentDirectory + @"\Lib\ip2region.xdb"));
二、下载“ip2region.db和ip2region.xdb
1、https://files.cnblogs.com/files/firstcsharp/ip2region.db.rar?t=1713425401&download=true
2、 https://gitee.com/lionsoul/ip2region#/lionsoul/ip2region/blob/master/binding/csharp 或 https://files.cnblogs.com/files/firstcsharp/ip2region.xdb.rar?t=1713425406&download=true
标签:files,ip2region,string,C#,IP2Region,IP地址,地及,new,ipRegion From: https://www.cnblogs.com/firstcsharp/p/18143593