C# HTTPContext.Current.Request Ip地址为空
我需要在asp.net控制器中获取客户端的IP地址 我使用以下代码:C# HTTPContext.Current.Request Ip地址为空,c#,asp.net,C#,Asp.net,我需要在asp.net控制器中获取客户端的IP地址 我使用以下代码: string ip=string.Empty; if(请求IsSecureConnection) ip=request.ServerVariables[“REMOTE_ADDR”]; if(string.IsNullOrWhiteSpace(ip)) { ip=request.ServerVariables[“HTTP_X_FORWARDED_FOR”]; if(string.IsNullOrWhiteSpace(ip)) {
string ip=string.Empty;
if(请求IsSecureConnection)
ip=request.ServerVariables[“REMOTE_ADDR”];
if(string.IsNullOrWhiteSpace(ip))
{
ip=request.ServerVariables[“HTTP_X_FORWARDED_FOR”];
if(string.IsNullOrWhiteSpace(ip))
{
ip=request.UserHostAddress;
}
其他的
{
if(ip.IndexOf(“,”,StringComparison.Ordinal)>0)
{
ip=ip.Split(',').Last();
}
}
}
有时ip
是空的。
你知道为什么吗?为什么
Httpcontext.Current.Request中的ip
为空?这是否回答了您的问题?我认为这是Web api,因为asp.net控制器可能是多种技术之一