Remotely connect to .net core self hosted web api

后端 未结 6 890
既然无缘
既然无缘 2020-12-29 02:11

I have a simple .net core web api with one action:

[Route(\"[action]\")]
public class APIController : Controller
{
    // GET api/values
    [HttpGet]
    pu         


        
6条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-29 02:35

    There is a more accurate way when there are multi IP addresses available on the local machine. Connect a UDP socket and read its local endpoint:

    string localIP;
    using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, 0))
    {
        socket.Connect("8.8.8.8", 65530);
        IPEndPoint endPoint = socket.LocalEndPoint as IPEndPoint;
        localIP = endPoint.Address.ToString();
    }
    

提交回复
热议问题