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;
}
これらの場所にファイルが存在することを確認しましたか?どちらの場合も、あなたはマスターという言葉のスペルを間違えました。また、ファイル名にスペースが含まれていますが、ここにコードを貼り付けるのが問題でしょうか? – ScoobyDrew18
ファイルは間違いなくroot〜/ Mater Pages(再命名することはできません)にあり、Mobile primary.masterファイルにはmobileとprimaryの間にスペースがあります。それがエラーの原因になるのでしょうか? –
@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) –