以下是移动设备检测代码,其包含三种不同的条件
if (Request.Browser.IsMobileDevice)
{
//Do Something
}
else if (((System.Web.Configuration.HttpCapabilitiesBase)myBrowserCaps).IsMobileDevice)
{
//Do Something
}
else if (IsMobileDevice(mobileDevices))
{
//Do Something
}
public bool IsMobileDevice()
{
private static string[] mobileDevices = new string[] {"iphone","ppc"
"windows ce","blackberry",
"opera mini","mobile","palm"
"portable","opera mobi" };
string userAgent = Request.UserAgent.ToString().ToLower();
return mobileDevices.Any(x => userAgent.Contains(x));
}
Run Code Online (Sandbox Code Playgroud)
我被迫写了三个条件,因为他们没有使用某些设备.
有谁能建议更好的方法?
我已经读过使用用户代理嗅探向移动浏览器发送正确内容的不好(不建议),所以我想知道最好的方法是什么?
我正在使用ASP.NET MVC,我已经构建了我的网站,它在桌面浏览器上运行良好,所以我希望开始构建移动版本.当移动浏览器进入我的网站时,我想使用一组不同的视图,理想情况下它们具有以下属性:
我的第一个想法是嗅探用户代理,然后发送一个不同的.CSS文件,但如上所述,我已经读到这是一个不好的方法,所以我问你的想法.