Startup 文件 注册服务
public void ConfigureServices(IServiceCollection services)
{ services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>(); }
获取IP地址
private readonly IHttpContextAccessor _httpContextAccessor; public HomeController(IHttpContextAccessor httpContextAccessor){
_httpContextAccessor = httpContextAccessor; } public object get(){ return _httpContextAccessor.HttpContext.Connection.RemoteIpAddress.MapToIPv4().ToString(); }