How does IsMobileDevice work?

前端 未结 7 1298
情话喂你
情话喂你 2020-11-28 11:01

MSDN makes it sound so easy to detect a mobile browser:

if (Request.Browser[\"IsMobileDevice\"] == \"true\" ) 
{
    Response.Redirect(\"MobileDefault.aspx\"         


        
相关标签:
7条回答
  • 2020-11-28 11:23

    I wouldn't rely on the MSDN link, there is no common standard unfortunately for mobile browsers and many try to mimic their non-mobile counterparts. Also it will return true if it doesn't recognize. See this link.

    0 讨论(0)
  • 2020-11-28 11:32

    Now, after 4 years, it's even more simple

    Request.Browser.IsMobileDevice
    
    0 讨论(0)
  • 2020-11-28 11:34

    A number of *.browser files are shipped with .NET:

    C:\Windows\Microsoft.NET\Framework\v2.0.50727\CONFIG\Browsers
    

    The runtime uses regular expressions from the *.browser files to match against the incoming User-Agent string, and then sets a bunch of properties based on each match it finds (there can be several in the hierarchy).

    If you need in-depth mobile device support, consider installing the MDBF, which adds support for about 400 devices:

    http://mdbf.codeplex.com/

    0 讨论(0)
  • 2020-11-28 11:38

    My current understanding is that there's just one exact solution to the problem of detecting whether a browser is mobile and next detecting its real capabilities. This solution is ScientiaMobile's WURFL (http://www.scientiamobile.com). Which, as of Aug30, is no longer free for every use. WURFL is now released with an ASP.NET API under AGPL. The data repository also comes with a specific license that disallows both copying and using with APIs different from the standard one (unless one purchases a commercial license).

    So for practical purposes other approaches such as 51Degrees cannot be used with more recent and future versions of the WURFL repository and this will make it difficult for 51Degrees to detect new devices (Windows Phone 7.5, for example).

    As for MDBF (a dismissed project), it may still work to detect whether a device is mobile (much better than the IsMobileDevice in ASP.NET). It is not entirely reliable as far as properties of the device are concerned. It insists that my HTC Desire Android has a 240x320 screen size, which is patently incorrect.

    My company bought a WURFL license and we're absolutely OK with that.

    0 讨论(0)
  • 2020-11-28 11:42

    Since for most sites, it is actually the size of the screen that matters and not so much the capabilities (at least when talking about modern phones with things like Safari and Chrome on them) wouldn't checking the resolution make the most sense?

    Request.Browser.ScreenPixelsHeight
    

    and

    Request.Browser.ScreenPixelsWidth
    
    0 讨论(0)
  • 2020-11-28 11:44

    Simply use below code,

    if (Request.Browser.IsMobileDevice) 
    {
        Response.Redirect("MobileDefault.aspx");
    }
    
    0 讨论(0)
提交回复
热议问题