Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# HTTPContext.Current.Request Ip地址为空_C#_Asp.net - Fatal编程技术网

C# HTTPContext.Current.Request 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)) {

我需要在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))
{
ip=request.UserHostAddress;
}
其他的
{
if(ip.IndexOf(“,”,StringComparison.Ordinal)>0)
{
ip=ip.Split(',').Last();
}
}
}
有时
ip
是空的。
你知道为什么吗?为什么
Httpcontext.Current.Request中的
ip
为空?

这是否回答了您的问题?我认为这是Web api,因为asp.net控制器可能是多种技术之一