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

2019-06-14 21:26发布

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.

5条回答
爷、活的狠高调
2楼-- · 2019-06-14 21:58
Request.ServerVariables["REMOTE_ADDR"]

To access an index or property on C#, you should use [ ] instead of ( )

查看更多
Animai°情兽
3楼-- · 2019-06-14 21:59
System.Web.HttpContext.Current.Request.UserHostAddress;
查看更多
欢心
4楼-- · 2019-06-14 22:04
 IpAddress=HttpContext.Current.Request.UserHostAddress;
查看更多
ら.Afraid
5楼-- · 2019-06-14 22:12

Use this code:

public static string GetIpAddress()
    {
        return HttpContext.Current != null ? HttpContext.Current.Request.UserHostAddress : "";
    }
查看更多
放我归山
6楼-- · 2019-06-14 22:14

One method is to use Request object:

protected void Page_Load(object sender, EventArgs e)
{
    lbl1.Text = Request.UserHostAddress;
}
查看更多
登录 后发表回答