WebEnh

.net7 mvc jquery bootstrap json 学习中 第一次学PHP,正在研究中。自学进行时... ... 我的博客 https://enhweb.github.io/ 不错的皮肤:darkgreentrip,iMetro_HD
  首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

.net core 获取本地ip及request请求端口

Posted on 2022-11-17 16:52  WebEnh  阅读(1341)  评论(0)    收藏  举报

1.获取ip和端口

string str = (Request.HttpContext.Connection.LocalIpAddress.MapToIPv4().ToString() + ":" + Request.HttpContext.Connection.LocalPort);

 

输出str,会得到当前服务器的IP及端口("127.0.0.1:5001")

2.获取ip

var ip = HttpContext.Request.Headers["X-Forwarded-For"].FirstOrDefault();
if (string.IsNullOrEmpty(ip))
{
    ip = HttpContext.Connection.RemoteIpAddress.ToString();
}
return ip;  "127.0.0.1"