C# 如何从 ASP.NET 中的请求中获取 IP 地址?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/866980/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you get the IP address from a request in ASP.NET?
提问by
I have been trying to figure this out but cannot find a reliable way to get a clients IP address when making a request to a page in asp.net that works with all servers.
我一直试图解决这个问题,但在向适用于所有服务器的 asp.net 中的页面发出请求时,找不到一种可靠的方法来获取客户端的 IP 地址。
回答by Jason
Request.ServerVariables["REMOTE_ADDR"]
To access an index or property on C#, you should use [ ] instead of ( )
要访问 C# 上的索引或属性,应使用 [ ] 而不是 ( )
回答by TheVillageIdiot
One method is to use Request object:
一种方法是使用 Request 对象:
protected void Page_Load(object sender, EventArgs e)
{
lbl1.Text = Request.UserHostAddress;
}
回答by Taran
IpAddress=HttpContext.Current.Request.UserHostAddress;
回答by Ankur vijay
Use this code:
使用此代码:
public static string GetIpAddress()
{
return HttpContext.Current != null ? HttpContext.Current.Request.UserHostAddress : "";
}
回答by Elnaz
System.Web.HttpContext.Current.Request.UserHostAddress;