2016-09-26 16 views
0

私は、サイトにアクセスするデバイスがモバイルデバイスであることが検出されたときに、動的に読み込むマスターページを取得しようとしていました。 しかし、モバイルまたはデスクトップシステムとしてデバイスが検出されたかどうかにかかわらず、デフォルトのprimary.masterが常にロードされるため、正しいマスターページをロードできないようです。動的にマスターページを読み込む際の問題

誰でも手助けできますか?

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Text.RegularExpressions; 

public partial class _Default : System.Web.UI.Page 


{ 
protected void Page_PreInt(object sender, EventArgs e) 
{ 
     if (Request.Browser.IsMobileDevice == true) 
     { 
      MasterPageFile = "~/Mater Pages/Mobile Primary.master"; 
     } 
     else 
     { 
      MasterPageFile = "~/Mater Pages /Primary.master"; 
      base.OnPreInit(e); 
     } 
    } 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     // If for any reason this page needs to be made inaccessible then remove the tags on either side of the text// 

     //Response.Redirect("~/Error Page.aspx");// 
    } 
} 
+0

これらの場所にファイルが存在することを確認しましたか?どちらの場合も、あなたはマスターという言葉のスペルを間違えました。また、ファイル名にスペースが含まれていますが、ここにコードを貼り付けるのが問題でしょうか? – ScoobyDrew18

+0

ファイルは間違いなくroot〜/ Mater Pages(再命名することはできません)にあり、Mobile primary.masterファイルにはmobileとprimaryの間にスペースがあります。それがエラーの原因になるのでしょうか? –

+0

@WillRダイナミックなマスターページバインディングのためには、ここに少し集中する必要があると思います(http://www.asp.net/web-forms/overview/older-versions-getting-started/master-pages/specifying -the-master-page-programmatically-cs)と[ここ](https://msdn.microsoft.com/en-us/library/c8y19k6h.aspx) –

答えて

1

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

信頼できるデバイス検出が必要な場合は、51Degreesなどの商用サービスを使用します。

イベントPage_PreInit(ないPage_PreInt)であるべきです。あなたはタイプミスがあります。

protected void Page_PreInit(object sender, EventArgs e) 
{ 
    // *** For debugging, I inverted if statement. You should do the same. **** 
    if (!IsMobileBrowser(HttpContext.Current)) 
     MasterPageFile = "~/MaterPages/Primary.master"; 
    else 
     MasterPageFile = "~/MaterPages/MobilePrimary.master"; 

    // *** You do not need to call base.OnPreInit(e); *** 
} 

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; 
} 
+0

アプリケーションと同じ問題でそれを試してみました。 –

+0

に関係なく、サイトはデフォルトのマスターページを使用し続けるIf文を逆転させましたか?また、ASPX内の '<%@ page %>'に*** MasterPageFile ***を明示的に割り当てていないことを確認してください。 – Win

+0

ASPXドキュメントをチェックしただけで、マスタページが具体的に記述されています。 –

関連する問題