有关动态加载母版页的问题

时间:2016-09-26 20:31:38

标签: c# asp.net

当检测到访问该网站的设备是移动设备时,我一直试图让主页动态加载。 但是,我似乎无法加载正确的母版页,因为它始终加载默认的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");//
    }
}

1 个答案:

答案 0 :(得分: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;
}