我们目前有一个大型 ASP.Net webforms 网站,我们刚刚开始将其中的一些部分转换为 asp.net MVc
我们想专门为移动客户端创建一个网站版本,并且我们想开始使用 MVC。
我知道 MVC 按照惯例支持以 .mobile 结尾的视图文件为移动客户端提供服务。但我的问题是 - 我一般如何设置桌面客户端仍然使用 default.aspx 但移动客户端被重定向到 /home
我们目前有一个大型 ASP.Net webforms 网站,我们刚刚开始将其中的一些部分转换为 asp.net MVc
我们想专门为移动客户端创建一个网站版本,并且我们想开始使用 MVC。
我知道 MVC 按照惯例支持以 .mobile 结尾的视图文件为移动客户端提供服务。但我的问题是 - 我一般如何设置桌面客户端仍然使用 default.aspx 但移动客户端被重定向到 /home
浏览器检测的代码应该放在一个基类中,所有页面都将从该基类中派生。试试下面的代码:
protected void Page_Load(object sender, EventArgs e)
{
if (IsMobileBrowser())
{
Response.Redirect("/home");
return;
}
// your other code
}
public static bool IsMobileBrowser()
{
//GETS THE CURRENT USER CONTEXT
HttpContext context = HttpContext.Current;
//FIRST TRY BUILT IN ASP.NT 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;
}
//AND FINALLY CHECK THE HTTP_USER_AGENT
//HEADER VARIABLE FOR ANY ONE OF THE FOLLOWING
if (context.Request.ServerVariables["HTTP_USER_AGENT"] != null)
{
//Create a list of all mobile types
string[] mobiles =new[]
{
"midp", "j2me", "avant", "docomo",
"novarra", "palmos", "palmsource",
"240x320", "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", "jb", "dddi",
"moto", "iphone"
};
//Loop through each item in the list created above
//and check if the header contains that text
foreach (string s in mobiles)
{
if(context.Request.ServerVariables["HTTP_USER_AGENT"].ToLower().Contains(s.ToLower()))
{
return true;
}
}
}
return false;
}
更多信息请访问:
http://www.codeproject.com/Articles/213825/ASP-net-Mobile-device-detection
http://www.codeproject.com/Articles/34422/Detecting-a-mobile-browser-in-ASP-NET
试试这样
if (request.Browser.IsMobileDevice)
{
//rediret the user to mobile views
}
确保删除 webform View Engine。像这样。移动兼容视图引擎
ViewEngines.Engines.Remove(ViewEngines.Engines.OfType<WebFormViewEngine>().First());
ViewEngines.Engines.Add(new MobileCapableWebFormViewEngine()); // this will come from Nuget Package.
将以下代码写入 Global.asax。
如果用户将通过桌面访问该网站,它将自动重定向到桌面网站天气用户通过 Android 手机访问它将自动重定向到移动网站
protected void Application_Start()
{
DisplayModes.Modes.Insert(0, new DefaultDisplayMode("Android")
{
ContextCondition = (context => context.Request.UserAgent.IndexOf
("Android", StringComparison.OrdinalIgnoreCase) >= 0)
});
DisplayModes.Modes.Insert(0, new DefaultDisplayMode("WindowsPhone")
{
ContextCondition = (context => context.Request.UserAgent.IndexOf
("Windows Phone OS", StringComparison.OrdinalIgnoreCase) >= 0)
});
}