如果您真的不能使用服务器变量,或者如果您需要地理信息以及 IP,您将需要以下内容:
<!-- Get geo information -->
<script type="text/javascript"
src="http://geoiplookup.wikimedia.org/">
</script>
<!-- JavaScript object-to-JSON -->
<!-- Don't actually hotlink this; host it yourself -->
<script type="text/javascript"
src="https://github.com/douglascrockford/JSON-js/blob/master/json2.js">
</script>
<!-- Hidden field to hold result -->
<asp:HiddenField id="hdnGeo" runat="server"></asp:HiddenField>
<!-- Script to populate field -->
<script type="text/javascript">
document.getElementById('<%= hdnGeo.ClientID %>').value =
JSON.stringify(Geo);
</script>
这会将 Geo 对象的 JSON 等效项放在您的 geoiplookup 参考中
{"city":"London","country":"GB","lat":"51.514198","lon":"-0.093100",
"IP":"193.129.26.250","netmask":"24"}
进入 ASP.NET 隐藏字段。
然后,在服务器上,您可以像这样访问它:
protected void Button1_Click(object sender, EventArgs e)
{
string json = hdnGeo.Value;
var dictionary = new JavaScriptSerializer()
.Deserialize<Dictionary<string, string>>(json);
string city = dictionary["city"];
string lat = dictionary["lat"];
string lon = dictionary["lon"];
string IP = dictionary["IP"];
string netmask = dictionary["netmask"];
}