How do you get the IP address from a request in AS

2019-06-14 22:01发布

问题:

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;