Asp.net core 获取客户机&x27;IP地址
以前在asp.net的其他版本中,我使用了Asp.net core 获取客户机&x27;IP地址,asp.net-core,ip-address,Asp.net Core,Ip Address,以前在asp.net的其他版本中,我使用了HttpRequest的以下属性: Request.ServerVariables["REMOTE_ADDR"] Request.UserHostAddress 如何在ASP.NET Core中实现同样的功能?HttpContext.Connection.RemoteIpAddress是您要查找的属性 您可以使用IHttpContextAccessor: 专用IHttpContextAccessor\u访问器; 公共Foo(IHttpContextAc
HttpRequest
的以下属性:
Request.ServerVariables["REMOTE_ADDR"]
Request.UserHostAddress
如何在ASP.NET Core中实现同样的功能?
HttpContext.Connection.RemoteIpAddress
是您要查找的属性
您可以使用IHttpContextAccessor
:
专用IHttpContextAccessor\u访问器;
公共Foo(IHttpContextAccessor访问器)
{
_存取器=存取器;
}
现在您可以通过这种方式获得IP地址”
var ip=\u accessor.HttpContext.Connection.RemoteIpAddress.ToString();
您可以使用请求
var GetIp = Request.HttpContext.Connection.RemoteIpAddress.ToString();
它工作得很好。
var ip = request.HttpContext.Connection.RemoteIpAddress;
我无法在DNX(完整clr)上获得此工作,有任何建议吗?RemoteIpAddress属性在我的应用程序中为空…@MarcinZablocki您必须在启动时注册它:
services.AddSingleton();