2016-09-26 40 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頁面中(沒有得到重命名),Mobile primary.master文件在移動設備和主要文件之間有一個空格。那可能是導致錯誤的原因嗎? –

+0

@WillR對於動態主頁面綁定,我認爲你需要進一步關注[這裏](http://www.asp.net/web-forms/overview/older-versions-getting-started/master-pages/specifying -the-master-page-programmatically-cs)和[here](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語句?另外請確保你沒有明確指派*** MasterPageFile ***在'<%@ page %>'直接在ASPX內部。 – Win

+0

剛剛檢查了ASPX文檔,它特別指出了母版頁。 –