Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does IsMobileDevice work?

Tags:

asp.net

mobile

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?

like image 343
Bryan Avatar asked Dec 01 '09 21:12

Bryan


4 Answers

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/

like image 111
RickNZ Avatar answered Nov 17 '22 21:11

RickNZ


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

Request.Browser.IsMobileDevice
like image 25
VladL Avatar answered Nov 17 '22 20:11

VladL


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
like image 2
CleverPatrick Avatar answered Nov 17 '22 19:11

CleverPatrick


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.

like image 1
Ta01 Avatar answered Nov 17 '22 20:11

Ta01