需要建议:C# Web 应用程序的地理定位 - 需要国家/地区位置来设置用户可以看到或看不到的内容

发布于 2024-12-07 08:14:15 字数 688 浏览 0 评论 0原文

我的情况是我想在用户访问网页时捕获用户的国家/地区。根据用户所在的国家/地区,我想设置一个 cookie,允许我在后面的代码中执行一个简单的“if”语句,让他们看到网页上的某些信息。 我开始做一些研究,我可以使用 Google Geocode V3 并从 C# 背后的代码中获取国家/地区。但为了让谷歌地理编码工作,我需要传递经度和纬度。 我看到这一点的唯一方法是通过 Javascript 调用,例如:

if(navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(position) {
        var latitude = position.coords.latitude;
        var longitude = position.coords.longitude;
    });
}

所以如果我做对了,我希望得到一些建议。 在我的 C# 代码中加载的方法上,我想我可以通过 ScriptManager.RegisterStartUpScript 方法运行 javascript。所以我现在的挑战是获取纬度和经度值。由于我不会执行按钮事件或其他事件,因此我需要获取这些值,然后将它们传递到我的 c# 方法中,该方法将使用 Google 地理编码获取国家/地区代码。 听起来对吗?如果这是正确的路径,有人可以建议我如何从 javascript 获取这些值吗? 谢谢。

My scenario is I want to trap the user's country when they access the web page. Based on the user's country i want to set a cookie that will allow me to do a simple "if" statement in the code behind that would let them to see certain information on web pages.
I started doing some research and i can use the Google Geocode V3 and get the country from code behind c#. But to get the Google Geocode to work i need to pass in the longitude and latitude.
And the only way i've seen this is from Javascript call like:

if(navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(position) {
        var latitude = position.coords.latitude;
        var longitude = position.coords.longitude;
    });
}

So i'm hoping for some advice if I'm doing this right.
On the loaded method in my c# code I think I can get the javascript to run by ScriptManager.RegisterStartUpScript method. So my challenge now is getting the latitude and longitude values. Since i'm not going a button event or something, i need to get those values out and then pass them into my c# method that would get the country code using the Google Geocode.
Does that sound right? If this is the right path, can someone suggest how i could get those values from the javascript?
thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你怎么敢 2024-12-14 08:14:15

您可以对包含客户端地理位置的服务器进行 Ajax 调用。

使用 RegionInfo.CurrentRegion 怎么样?假设您将全球化文化转向汽车,这将根据浏览器标题为您提供国家/地区。这更简单,但也不会始终 100% 正确,因为用户可以更改其文化设置。

您可能还想考虑使用基于 IP 地址的地理定位,这可能会更准确一些。

了解您为什么要尝试这样做会有所帮助。

You could make an Ajax call to your server which contains the client's geolocation.

What about using RegionInfo.CurrentRegion instead? That will give you the country according to the browser headers assuming you have globalization culture turned to auto. This is simpler, but will also not be 100% correct all the time since the user can change their culture settings.

You may also want to look into using IP address based geolocation which might be a little more accurate.

It would help to know why you're trying to do this.

北笙凉宸 2024-12-14 08:14:15

大多数真正需要确保内容不超出国界的网站都会处理收费内容 - 例如,大多数音乐网站、亚马逊等。

他们简单而有效的技术是使用用户信用卡上的帐单地址所在的国家/地区。

Most sites that really need to make sure that content stays within national boundaries work with content that is chargeable - for instance, most music sites, Amazon, and so on.

Their simple, yet effective, technique is to use the country on the user's billing address on their credit card.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文