当前位置:主页 > 软件编程 > .NET代码 >

.NET6使WebApi获取访问者IP地址

时间:2022-11-20 10:13:31 | 栏目:.NET代码 | 点击:

Program.cs文件中

//获取IP使用
builder.Services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();

Controller中

private readonly IHttpContextAccessor httpContextAccessor;
public YangController(IHttpContextAccessor _httpContextAccessor)
{
  httpContextAccessor = _httpContextAccessor;
}

var ip=httpContextAccessor.HttpContext.Connection.RemoteIpAddress.ToString();

您可能感兴趣的文章:

相关文章