How do you get the IP address from a request in ASP.NET?

大兔子大兔子 提交于 2019-12-23 06:47:34

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!