2017-06-21 3 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; 
} 
関連する問題