.net 5.0获取客户端IP

相关视频教程 https://www.bilibili.com/video/BV1LM4y1V7Am(如果不会请看我录制的视频教程

1、在Startup类里面 注册服务,如下图


services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();


2、在控制器里面获取ip 方法如下


HttpContextAccessor context = new HttpContextAccessor();
string ip = context.HttpContext?.Connection.RemoteIpAddress.ToString();
ViewData["ip"] = ip;


评论

  1. 203.168.7.*2021-11-23 09:01:35
    不错