如何在asp.net中获取ip地址

如何在asp.net中获取ip地址,asp.net,ip-address,Asp.net,Ip Address,大家好,我正在用下面的c代码检索我的ip地址,我得到的只是127.0.0.0。IP地址。我需要显示我的IPA,就像我键入我的ip地址时在google searh上显示的一样。 你能帮忙吗?多谢各位 HttpContext.Current.Request.ServerVariables("REMOTE_ADDR") Request.ServerVariables("REMOTE_HOST") Request.UserHostAddress() Request.UserHostName() st

大家好,我正在用下面的c代码检索我的ip地址,我得到的只是127.0.0.0。IP地址。我需要显示我的IPA,就像我键入我的ip地址时在google searh上显示的一样。 你能帮忙吗?多谢各位

HttpContext.Current.Request.ServerVariables("REMOTE_ADDR")
Request.ServerVariables("REMOTE_HOST")
Request.UserHostAddress()
Request.UserHostName()


string strHostName = System.Net.Dns.GetHostName(); 
string clientIPAddress = System.Net.Dns.GetHostAddresses(strHostName).GetValue(0).ToString();

我也尝试过这个方法,但它引发了一个异常“仅支持ipv4”

您也可以尝试以下方法:

using System;
使用System.Web

namespace WebApplication1
{


}

试试Request.ServerVariables(“HTTP_X_FORWARDED_FOR”),你是否有代理?另外,如果你在本地机器上测试,你会得到环回地址。感谢GalaxixCowboy,你说得对,我将它部署在我的web主机上,它工作正常。你才是真正的克林特·伊斯特伍德
 public class Global : HttpApplication
{
protected void Application_BeginRequest(object sender, EventArgs e)
{
    // Get request.
    HttpRequest request = base.Request;

    // Get UserHostAddress property.
    string address = request.UserHostAddress;

    // Write to response.
    base.Response.Write(address);

    // Done.
    base.CompleteRequest();
}
}