43

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

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

Actually, it looks like you can also just check Request.Browser.IsMobileDevice. But how does this actually work? I don't even have a .browser file... what's going on behind the scenes here? Are there some built-in defaults for ASP.NET 2.0?

Bryan
  • 8,748
  • 7
  • 41
  • 62

7 Answers7

62

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/

RickNZ
  • 18,448
  • 3
  • 51
  • 66
  • 50
    So you downvoted a 2+ year old answer because the library I reference is now old? You're going to have your hands full with other old answers on SO! And if you're going to go there, you may as well go the rest of the way: server-side browser ID is now old. There's very good client-side support now (such as Modernizr), which will only get better as support for CSS3 improves. – RickNZ Feb 22 '12 at 11:39
  • 17
    I think James Rosewell is promoting a product. – J. Polfer Jun 28 '13 at 19:58
17

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

Request.Browser.IsMobileDevice
VladL
  • 12,769
  • 10
  • 63
  • 83
  • 3
    Any idea on how this property works? The MSDN article on it just says it knows (essentially). – Rafiki Sep 08 '17 at 18:29
2

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
CleverPatrick
  • 9,261
  • 5
  • 63
  • 86
  • I was about to down vote this, but I realize that in my tests I am using .net 3.5. Is this corrected in .net 4.0? I haven't checked, but in .net 3.5 it always returns the same number regardless of what device. I have read articles that one should use JavaScript to detect this data. I am imagine that this could be spoofed too, no? Well anything can be spoofed really. – pqsk May 28 '12 at 19:06
  • It's true that anything can be spoofed and anything can be inaccurate. All the Browser.* variables work based on information passed as part of the User Agent. Something which many browsers allow the spoofing of as a feature of the browser! – CleverPatrick May 29 '12 at 17:46
  • 2
    I was thinking the same thing, but this gives me 640x480 on everything. – Dave Cousineau Sep 02 '12 at 23:17
  • 8
    This won't work because you could have a small screen with a very high resolution. – mhenry1384 Oct 11 '12 at 18:19
1

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.

Ta01
  • 31,040
  • 13
  • 70
  • 99
  • Did you mean to say it returns false if it doesn't recognize? I am using this feature on my site and I am testing and on my phone it returns false and when I mimic iOS phones on my browser it returns false. I'm using .net 3.5 on my website. Just curious. – pqsk May 28 '12 at 19:03
1

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

Issues has been resolved while I added 51Degrees, just add 51degrees using Nuget thats all. See more here https://51degrees.codeplex.com

Shahdat
  • 5,343
  • 4
  • 37
  • 37
-2

Simply use below code,

if (Request.Browser.IsMobileDevice) 
{
    Response.Redirect("MobileDefault.aspx");
}
Tahir Alvi
  • 896
  • 2
  • 14
  • 44