2017-06-21 36 views
0

我想知道用户来自桌面还是来自移动设备。 如果它来自移动设备 - 重定向到进程。如何重定向到流程去?

你能帮助我吗?

+0

我不知道“重定向到流程去”是什么意思。你能澄清一下吗?为什么不建立一个响应式网站而不是在其他地方发送移动用户呢?这与桌面应用程序标签有什么关系? – mason

回答

0

如果您只想知道布尔值值,则可以使用以下帮助程序方法。

如果您想了解有关所需设备的更多详细信息,您需要使用第三方库,如​​。

public static bool IsMobileBrowser(HttpContext context) 
{ 
    // first try built in asp.net check 
    if (context.Request.Browser.IsMobileDevice) 
    { 
     return true; 
    } 

    // then try checking for the http_x_wap_profile header 
    if (context.Request.ServerVariables["HTTP_X_WAP_PROFILE"] != null) 
    { 
     return true; 
    } 

    // then try checking that http_accept exists and contains wap 
    if (context.Request.ServerVariables["HTTP_ACCEPT"] != null && 
     context.Request.ServerVariables["HTTP_ACCEPT"].ToLower().Contains("wap")) 
    { 
     return true; 
    } 

    // Finally check the http_user_agent header variable for any one of the following 
    if (context.Request.ServerVariables["HTTP_USER_AGENT"] != null) 
    { 
     // List of all mobile types 
     string[] mobiles = 
      new[] 
      { 
       "android", "opera mini", "midp", "j2me", "avant", "docomo", "novarra", "palmos", "palmsource", 
       "240×320", "opwv", "chtml", 
       "pda", "windows ce", "mmp/", "blackberry", "mib/", "symbian", "wireless", "nokia", "hand", "mobi", 
       "phone", "cdm", "up.b", "audio", "sie-", "sec-", "samsung", "htc", "mot-", "mitsu", "sagem", "sony", 
       "alcatel", "lg", "eric", "vx", "nec", "philips", "mmm", "xx", "panasonic", "sharp", "wap", "sch", 
       "rover", "pocket", "benq", "java", "pt", "pg", "vox", "amoi", "bird", "compal", "kg", "voda", 
       "sany", "kdd", "dbt", "sendo", "sgh", "gradi", "dddi", "moto", "iphone" 
      }; 

     // Check if the header contains that text 
     var userAgent = context.Request.ServerVariables["HTTP_USER_AGENT"].ToLower(); 

     return mobiles.Any(userAgent.Contains); 
    } 

    return false; 
}