Program.cs文件中
//获取IP
builder.Services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
Controller中
private readonly IHttpContextAccessor httpContextAccessor;
public XXXXXXXController(IHttpContextAccessor _httpContextAccessor)
{
httpContextAccessor = _httpContextAccessor;
}
Controller中需要使用的地方
[HttpGet]
[Route("XXXXXXXXXXX")]
public voild XXXXXXXXX()
{
//获取用户ip地址
var ip= httpContextAccessor.HttpContext.Connection.RemoteIpAddress.ToString();
}