获取WEB API中连接到网络的计算机的本地IP

时间:2015-11-24 06:21:32

标签: asp.net-mvc asp.net-web-api

我正在尝试将本地IP机器连接到ASP.Net WEB API中的网络。是否有任何方法可以做到这一点?

1 个答案:

答案 0 :(得分:4)

You can get local IP of machine following way.Thanks

    private string LocalIPAddress()
       {
            IPHostEntry host;
            string localIP = "";
            host = Dns.GetHostEntry(Dns.GetHostName());
            foreach (IPAddress ip in host.AddressList)
            {
                if (ip.AddressFamily == AddressFamily.InterNetwork)
                {
                    localIP = ip.ToString();
                }
            }
            return localIP;
        }