获取IP地址,浏览器类型MVC 5

时间:2015-06-22 05:59:02

标签: asp.net-mvc-5

我需要在MVC 5中获取本地系统IP地址和浏览器代理(firefox,chorme,ie等)。搜索google Request.ServerVariables [“REMOTE_ADDR”],这在MVC5中不起作用

3 个答案:

答案 0 :(得分:3)

获取IP地址使用此代码:

Part * walker = firstPointer;
Part * stalker = NULL;

if(walker == NULL){
    cout << "Empty list" << endl;

    return;
}

while (walker != NULL){
    //stalker = walker; //not here!
    if (partNum == walker->partNo){
        if(walker == firstPointer){ //first node
            //do your stuff, delete walker
            ...
            firstPointer = NULL;
        }
        else{
            stalker->link = walker->link;
            //do stuff, delete walker
            ...
         }

        return;
    }
    stalker = walker; //here!
    walker = walker->link;
}

cout << "Part Number Not Found..." << endl;

https://stackoverflow.com/a/7348761/4568359

=============================================== =================

获取浏览器信息:

public static string GetIPAddress(HttpRequestBase request)
    {
        string ip;
        try
        {
            ip = request.ServerVariables["HTTP_X_FORWARDED_FOR"];
            if (!string.IsNullOrEmpty(ip))
            {
                if (ip.IndexOf(",") > 0)
                {
                    string[] ipRange = ip.Split(',');
                    int le = ipRange.Length - 1;
                    ip = ipRange[le];
                }
            }
            else
            {
                ip = request.UserHostAddress;
            }
        }
        catch { ip = null; }

        return ip;
    }

https://msdn.microsoft.com/en-us/library/3yekbd5b.aspx

答案 1 :(得分:1)

您正在寻找类似于获取IP地址的内容

HttpRequest.UserHostAddress Property

并查看此内容以获取浏览器检测51Degrees.Mobi Foundation

答案 2 :(得分:1)

获取客户端IP地址

var IPAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (string.IsNullOrEmpty(IPAddress))
{
    IPAddress = Request.ServerVariables["REMOTE_ADDR"];
}

获取客户端用户代理。

var userAgent = Request.UserAgent;