Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC 3 simple and reliable mobile device detection: use 51degrees.mobi or go native?

In my ASP.NET MVC 3 web application I am overriding the View name in a custom view engine to present a different view optimized for non-tablet mobile devices.

All I want to perform this view override is an accurate value for Request.Browser.IsMobileDevice and the device's native display width.

51degrees.mobi feels very heavy and convoluted for this simple use case. Maybe that is just my frustration at trying to wade through all of the examples and documentation on the 51degrees web site to get answers to what seem like simple questions for a simple implementation.

Absent 51degrees, I do get values for Request.Browser.IsMobileDevice and Request.Browser.ScreenPixelsWidth. I've noticed that for desktop browsers the width is always 640 whereas Request.Browser.Capabilities["51Degrees.mobi"] contains a value of "Unknown" for ScreenPixelsWidth. I guess the Unknown value is more accurate given the server-side nature of the implementation.

My main question is: do I really need 51degrees for my simple requirements?

If I don't use 51degrees, are there some specific examples of browsers that I will fail to detect IsMobileDevice and the screen width correctly? For example will the built-in .NET Request.Browser.IsMobile and ScreenPixelsWidth values be inaccurate or absent for the new iPad when it comes out or for the latest Android browser version? My site gets about 65% mobile users, many on lame feature-phones with browsers like the UP.Browser, so getting mobile right is somewhat important.

If I use 51degrees, do I need to always drill down into Request.Browser.Capabilities["51Degrees.mobi"]? Or does 51degrees override/update the value of Request.Browser.IsMobileDevice to be in synch with its evaluation of the browser? Clearly it does not override Request.Browser.ScreenPixelsWidth.

I am open to answers of "you are doing it wrong, here is a better way" as long as they are not suggesting MVC 4 or involving a paid solution such as WURFL.

like image 985
Pat James Avatar asked Mar 14 '12 17:03

Pat James


1 Answers

This FAQ explains which properties of Request.Browser are overridden by 51Degrees.mobi.

There are very few cases where you should ever access the "51Degrees.mobi" key directly. Where the property has been overridden access it as you normally would. Where the property is not overridden or not covered by HttpBrowserCapabilities you should use the property name. For example:

var isTablet = Request.Browser["IsTablet"];

See this link for a list of available properties.

As it should overriden ScreenPixelsWidth I'd be interested to know more about your test environment. As .NET requires the value to be numeric if 51Degrees.mobi can not determine the size (Unknown) then the value will not be overridden.

Thank you for considering 51Degrees.mobi.

like image 197
James Rosewell Avatar answered Oct 17 '22 02:10

James Rosewell