How to detect visitor country

橙三吉。 提交于 2019-12-02 20:13:41

问题


To detect a visitor country I see this below code suggested in many forums, but I cant get it working.

modGlobal.ResolveCountry.ThreeLetterISORegionName

On my local machine it correctly return my computer retional settings region whereas it on the production server always return USA.

I guess this is because the function return the envoirement regional settings (ie the servers regional setting), can anyone confirm this? And if true, what is best practice for detecting visitors country in asp.net?


回答1:


Try this

Dictionary<string,string> objDic = new Dictionary<string,string>();

foreach (CultureInfo ObjCultureInfo in CultureInfo.GetCultures(CultureTypes.SpecificCultures))
{
RegionInfo objRegionInfo = new RegionInfo(ObjCultureInfo.Name);
if (!objDic.ContainsKey(objRegionInfo.EnglishName))
  {
      objDic.Add(objRegionInfo.EnglishName, objRegionInfo.TwoLetterISORegionName.ToLower());
  }
}

var obj = objDic.OrderBy(p => p.Key );
foreach (KeyValuePair<string,string> val in obj)
{
  ddlCountries.Items.Add(new ListItem(val.Key, val.Value));
}

EnglishName will return country name

From the IP see




回答2:


Try to get the ip from the visitor and look up the trace data from it

Maybe have a look at this: How to get visitor location ( country, state and city ) using ASP.NET



来源:https://stackoverflow.com/questions/17588228/how-to-detect-visitor-country

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!