2017-06-21 69 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; 
}