如何检测平板电脑(任何)请求

本文关键字:平板电脑 任何 请求 检测 何检测 | 更新日期: 2023-09-27 17:58:52

 if (Request.Browser.IsMobileDevice)
 {
     Response.Redirect("/mobile/Login.htm");`
 }

要检测手机,但同时检测类似平板电脑的手机,我需要检查是否有平板电脑的功能或检查设备屏幕大小的功能。

感谢我使用的ScreenPixelsWidth和ScreenPixelHeight这是代码,如果有需要的话

 int wight = Request.Browser.ScreenPixelsWidth;
                int height = Request.Browser.ScreenPixelsHeight;
                if (Request.Browser.IsMobileDevice && wight < 720 && height<1280)
            {
               Response.Redirect("/mobile/Login.htm");
            }

如何检测平板电脑(任何)请求

我遇到了类似的问题,并尝试使用:HttpContext。要求浏览器屏幕像素宽度

然而,无论设备(iphone或ipad(如何,它总是返回640像素的值。我通过创建一个静态方法来检查User Agent字符串来解决这个问题。

public class DeviceHelper
{
    public static bool IsTablet(string userAgent, bool isMobile)
    {
        Regex r = new Regex("ipad|android|android 3.0|xoom|sch-i800|playbook|tablet|kindle|nexus");
        bool isTablet = r.IsMatch(userAgent) && isMobile;
        return isTablet;
    }
}

然后在我的控制器中:

if(DeviceHelper.IsTablet(Request.UserAgent, Request.Browser.IsMobileDevice))
     return Redirect("..."); // redirect to tablet url

您可以使用ScreenPixelsWidth和ScreenPixlsHeight(http://msdn.microsoft.com/en-us/library/system.web.httpbrowsercapabilities.aspx)并且您可以定义一个阈值,在该阈值中您可以考虑应该呈现常规版本还是移动版本。

有很多方法可以解决这个问题,但由于您已经在使用HttpBrowserCapabilities类,您还可以使用这两个属性。

ScreenPixelsWidth总是返回640,因此在检测手机时没有用处。我发现了这个作品:

public static bool IsPhoneDevice(this HttpBrowserCapabilitiesBase Browser)
{
      return (Browser.IsMobileDevice && Browser.CanInitiateVoiceCall);
}