2016-10-26 9 views
0

私のウェブサイトでは、新しいWebフォームソリューション(つまり、Site.Master & Site.Mobile.Master)でデフォルトで作成された2つのマスターページを使用していました。私は私のモバイルマスターページがロードされることはなく、むしろ「通常の」マスターページであることをモバイル用のfirefoxに気付きました。モバイルやその他のブラウザ用のクロムを使用すると、すべて正常に動作します。 私の解決策に実質的な変更を加えましたが、モバイルマスターページはどのような場合でもデフォルトでロードされることはありません(クローム開発ツールが含まれています)。ここでの問題は、私はすべての単一のaspxページに含める必要があるということですモバイルマスターページが自動的に起動されない

protected void Page_PreInit(object sender, EventArgs e) 
    { 
     if (Request.Browser.IsMobileDevice) 
     { 
      MasterPageFile = "~/Site.Mobile.Master"; 
     } 
    } 

:私はクロームで正常に動作しますが、まだFirefoxにはfalseを返します。このソリューションに私を導いたいくつかの研究を行っています。私は多くの研究をしており、それに関する文書が非常に貧しいことを認めなければなりません。 web.configファイルに追加できる設定や、global.asaxに追加するコードがないので、すべてのaspxページでブラウザを確認する必要はありませんか?

答えて

1

Request.Browser.IsMobileDeviceは信頼できるものではありません。次のヘルパーメソッドは少し多くを検出することができます。

新しいデバイスを含むすべてのデバイスで確実に検出するには、51Degreesなどの商用サービスを使用します。

protected void Page_PreInit(object sender, EventArgs e) 
{ 
    // *** For debugging, I inverted if statement. 
    //  You can reverse it back after debugging. **** 
    if (!IsMobileBrowser(HttpContext.Current)) 
     MasterPageFile = "~/Site.Desktop.Master"; 
    else 
     MasterPageFile = "~/Site.Mobile.Master";  
} 

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