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();
到此这篇关于.NET6使WebApi获取访问者IP地址的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持编程网。