问题
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.
回答1:
One method is to use Request object:
protected void Page_Load(object sender, EventArgs e)
{
lbl1.Text = Request.UserHostAddress;
}
回答2:
IpAddress=HttpContext.Current.Request.UserHostAddress;
回答3:
Request.ServerVariables["REMOTE_ADDR"]
To access an index or property on C#, you should use [ ] instead of ( )
回答4:
Use this code:
public static string GetIpAddress()
{
return HttpContext.Current != null ? HttpContext.Current.Request.UserHostAddress : "";
}
回答5:
System.Web.HttpContext.Current.Request.UserHostAddress;
来源:https://stackoverflow.com/questions/866980/how-do-you-get-the-ip-address-from-a-request-in-asp-net