C# asp.net中的移动设备检测
以下是包含三种不同条件的移动设备检测代码C# asp.net中的移动设备检测,c#,asp.net,C#,Asp.net,以下是包含三种不同条件的移动设备检测代码 if (Request.Browser.IsMobileDevice) { //Do Something } else if (((System.Web.Configuration.HttpCapabilitiesBase)myBrowserCaps).IsMobileDevice) { //Do Something } else if (IsMobileDevice(mobileDevices)) { //Do Something }
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));
}
我被迫写了三个条件,因为有些设备出现故障
有人能推荐一个更好的方法吗?从以下代码中选取并更改一点: 我从
http://detectmobilebrowsers.com/
- 使静态编译Regex以加快检查速度
- 修复u少于4个字符时的错误
- 其他支票
这段代码速度很快,其中一个技巧是我们已经对正则表达式进行了静态和预编译。在第一次检查之后,我建议在用户会话上保存结果,并使用更快的会话变量。是的,如果您想确定useragent可能是最好的方法。您还可以查看此方法:
它应该有一些新浏览器之类的问题。但这是你目前正在做的一件事。斯科特·汉斯曼在这个问题上有着伟大的见解。他提到了一个名为51Degrees的第三方解决方案。我建议将用户代理列表存储在web.config中,以便于配置
IsMobileDevice
不会随新浏览器进行主动更新,因此它确实存在局限性,这就是您需要进一步检查的原因
该软件包将是一个更可靠的替代方案。现有的浏览器定义非常糟糕。理想情况下,您希望使用来扩充这些定义 幸运的是,这很容易使用 还有一个:
检查codebehind中的UserAgent是否有类似于引用的javascript的“Mobi”
这段代码适用于我的情况
public void IsMobileBrowser()
{
String labelText = "";
System.Web.HttpBrowserCapabilities myBrowserCaps = Request.Browser;
if (((System.Web.Configuration.HttpCapabilitiesBase)myBrowserCaps).IsMobileDevice)
{
labelText = "Browser is a mobile device.";
}
else
{
labelText = "Browser is not a mobile device.";
}
ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('"+ labelText + "');", true);
}
参考资料:我正在使用DeviceDetector.NET,因为我经常需要将智能手机和平板电脑分开 安装它
安装软件包DeviceDetector.NET
然后像这样使用它
DeviceDetectorSettings.RegexesDirectory = "C:\\DeviceDetector.NET.3.11.4\\";
var dd = new DeviceDetector(Request.UserAgent);
dd.Parse();
var device = dd.GetDeviceName();
if(device == "tablet") {}
if(device == "smartphone") {}
if(device == "desktop") {}
/**
Possible returns
--
desktop
smartphone
tablet
feature phone
console
tv
car browser
smart display
camera
portable media player
phablet
**/
正则表达式可以在packages目录中找到我对这个库很感兴趣。它一直在我的脑海中挥之不去CPU@Aristos:我试了两次,重新编译了所有内容;删除了所有的旧东西。同样的问题(事实上,我昨天已经这么做了,因为我对糟糕的表现感到非常惊讶——否则我就不会发布了)。@johngrinder我相信你,但你可能还有其他问题。。。我的网站,使用这个程序和其他非常快。。。因为我缓存了结果,如果我找到时间,我会计时并返回结果。@Aristos没有检测到Andriod版本2.3.5HTC 3.0软件编号3.13.661.4为mobile@Apollo在第一次呼叫主页时,或在第一次呼叫时,在您网站的每个页面上-在那里,您检查这是否是手机,并在“相同”上重定向它们如果可以的话,手机页面-主页是最简单的一个,因为你只有一张支票,而且你知道把它们寄到哪里。。。我只对每个用户进行一次检查,然后将结果存储在会话中,不再检查!我还把代码放在一个dll库中,我在网站上使用这个库,放在一个全局静态类中。这对我很有帮助。你能告诉我为什么它们应该是静态的吗?如果每秒有1000个以上的请求,这里是否存在静态生成问题?你能告诉我这里的静态用法吗?谢谢。从用户代理字符串中检测设备可能会重复。这不像看上去那么容易,因为浏览器使用的是谎言。我正在为.NET移植一个基于Antlr运行时的真正解析器,您可以看看:why
Request.Browser.IsMobileDevice
vs((System.Web.Configuration.HttpCapabilitiesBase)myBrowserCaps).IsMobileDevice
?由于请求。浏览器。IsMobileDevice未能检测到某些设备,这就是我建议使用此选项@kiquenett的原因。此方法解决了我的问题。
Install-Package 51Degrees.mobi
if (Request.UserAgent.Contains("Mobi") == true)
{
DivToShowWhenMobileDevice.Style.Add("display", "");
}
else
{
DivToShowWhenMobileDevice.Style.Add("display", "none");
}
public void IsMobileBrowser()
{
String labelText = "";
System.Web.HttpBrowserCapabilities myBrowserCaps = Request.Browser;
if (((System.Web.Configuration.HttpCapabilitiesBase)myBrowserCaps).IsMobileDevice)
{
labelText = "Browser is a mobile device.";
}
else
{
labelText = "Browser is not a mobile device.";
}
ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('"+ labelText + "');", true);
}
DeviceDetectorSettings.RegexesDirectory = "C:\\DeviceDetector.NET.3.11.4\\";
var dd = new DeviceDetector(Request.UserAgent);
dd.Parse();
var device = dd.GetDeviceName();
if(device == "tablet") {}
if(device == "smartphone") {}
if(device == "desktop") {}
/**
Possible returns
--
desktop
smartphone
tablet
feature phone
console
tv
car browser
smart display
camera
portable media player
phablet
**/