Igor Kulman

Detecting tablets and smartphones in ASP.NET

· Igor Kulman

I recently worked on an ASP.NET application that needed to detect if users were coming from tablets or smartphones. The project used data from http://user-agent-string.info/ to do this detection, but the result were not really good. We needed a better solution, so I came up with using WURFL.

WURFL, the Wireless Universal Resource FiLe, is a Device Description Repository (DDR), i.e. a software component that maps HTTP Request headers to the profile of the HTTP client (Desktop, Mobile Device, Tablet, etc.) that issued the request. Adding WURFL to your ASP.NET application is easy thanks to the WURFL_Official_API Nuget package. The Nuget package also contains definition file, so you just need to update the Nuget package once in a while to get your definition file up to date.

After installing the Nuget package, you need to setup WURLF in your Global.asax file

var wurflDataFile = HttpContext.Current.Server.MapPath("~/App_Data/wurfl-latest.zip");
var configurer = new InMemoryConfigurer().MainFile(wurflDataFile).SetMatchMode(MatchMode.Accuracy);
WURFLManagerBuilder.Build(configurer);

I recommend setting the match mode to accuracy instead of speed, to get the best results. Using the WURFL library is also quite easy, just pass the user agent string and get the properties you want.

var device = WURFLManagerBuilder.Instance.GetDeviceForRequest(context.Request.UserAgent);
var isTablet = Boolean.Parse(device.GetCapability("is_tablet"));
var isMobileDevice = Boolean.Parse(device.GetCapability("is_smartphone"));

See also