• About Us
  • Blog
  • Basket
  • Account
  • Sign In


Published on Thursday, July 21, 2011

51Degrees.mobi Mobile Detection Outside of IIS

Edit: I wrote this blog short after we released version 1.2. We have now released version 2.0 using our own 51Degrees.mobi that required a few api tweaks. The source code and this article have been rewritten for 2.0; so if you're reading this for referencing to update a 1.x version of the Foundation we highly recommend you upgrade.

51Degrees.mobi has a proven record of fast, reliable detection on ASP.NET website but it has always been restricted to environments within IIS. The 1.2 release has now changed this and detection can now be used offline outside of IIS from any .NET code. To show how, I have made two example projects showing local based detection; a lightweight command line implementation, and a slightly more advanced GUI driven based version. You will also need 51Degrees.mobi Device Data to use with the project. You can download it here.

But first some code:

static void Main(string[] args)
   //Creates an instance of the device data detection provider
   var Provider = FiftyOne.Foundation.Mobile.Detection.Binary.Reader.Create(DataFilePath);
   //Creates DeviceInfo objects to obtain data from.
   BaseDeviceInfo deviceInfo = Provider.GetDeviceInfo(Device);
         "The brand name is '{0}'. Press a key to finish.",

This example has been up on Codeplex since the 1.2 release. Normally, the Foundation is a complex beast with many configuration options when used with a website, but here only two objects matter: The Provider and DeviceInfo.

The Provider interprets the wurfl files and makes data available for DeviceInfo, the bit which gets all the data for the particular device being queried.

The code is simple enough so let’s move on to setting up a project to use the FiftyOne namespace.

Creating a Project

The first step is to download FiftyOne.Foundation.DLL. It can be found in any of the projects on Codeplex or compiled from the source code. After that is has to be referenced from Visual Studio. Create a project (any .NET project will do, but this article uses the console C# template). Right click on References in the Solution Explorer and then click ‘Add Reference…’ and navigate to the DLL to add it. You may want copy a version of the DLL to the project directory.

Figure 1 - Adding a reference in Visual Studio 2010

You will now be able to use the FiftyOne namespace. The Reader and DeviceInfo object themselves are in the namespace FiftyOne.Foundation.Mobile.Detection.

There are now two different Reader objects, one for binary and xml data formats. The location of the file is given to the Reader constructor.

The code so far is with file checking:

using System;
using System.IO;
using System.Web;
using FiftyOne.Foundation.Mobile.Detection;
using System.Collections.Generic;
namespace Detect
    public class GetCapability
        /// Device data file path.
        const string DataFilePath = @"App_Data/51Degrees.mobi-Lite.dat";
        /// Get data about a particular capability or property of a device.
        /// The name of the Device to query.
        /// The name of the Capability of Property to retrieve data about.
        public static string AboutDevice(string Device, string Capability)
            if (File.Exists(DataFilePath))
                //Creates provider
                var Provider = FiftyOne.Foundation.Mobile.Detection.Binary.Reader.Create(DataFilePath);
                //If you would prefer to use the xml format then use this code
                //var xmlList = new List();
                //var Provider = FiftyOne.Foundation.Mobile.Detection.Xml.Reader.Create(xmlList);
                //Creates DeviceInfo objects to obtain data from.
                BaseDeviceInfo deviceInfo = Provider.GetDeviceInfo(Device);
                //Will be an empty string if either a capability or device wasn't identified.
                return deviceInfo.GetFirstPropertyValue(Capability);
            throw new FileNotFoundException("A data file was not found. You can download a Lite data file from http://51degrees.codeplex.com", DataFilePath);

This code is accepted as being fine in the IDE, but when executed…

Figure 2 - Warnings

It seems odd that FiftyOne.Foundation couldn’t be found when it was referenced but this because it depends on System.Web to function. Add ‘using System.Web’ and it should work. If System.Web isn’t available it is probably because you are using .NET Client Profile Framework. To change it right click on the project file and choose a different target framework:

Figure 3 - Changing Target Framework

And the code should now compile.

Example Detectors

Alternatively, if you don’t fancy writing the code, here is a solution that includes two projects demonstrating the code. Both of these programs use the same logic and they both need access to the wurfl files. By default the files are stored in a folder titled App_Data next to the executable.

Command Line Detector:

This program takes the first argument as the name of the device and the second as the name of the capability to be found. This program could be used in batch processes or called from other programs. This way detection can be used from other programming languages without using the .NET runtime.

Detection GUI

This program uses the same logic as the command line but presents it in a slightly prettier way.

Download the source code here. To get it working you will need to download device data. You can get it from Codeplex.

If you having problems retrieving data, bear in mind that the capability needs to be a wurfl one. A complete list can be found here. Also remember that it identifies by useragent. iPad is a nice example of when a device name will be matched, but this isn’t always the case. ipad, for instance (lower-case P) will not be identified, as an iPad useragent always has the capital P.

And there it is, an easy way to get reliable device detection without leaving the comfort of your local system.

Comments (0)

Author: Anonym

Categories: Development

Tags: Device Detection , .NET



.NET 4G 51Degrees 5G Acer Adform Adtech Advertising Afilias Alcatel Amazon AMP Analysis Analytics Android Apache API Apple Asian Market ASP.NET Asus Blackberry Browser C C# Centro Chrome chromium Client hints Cloud CMS CPU CSS3 Data Data Blog Data File Daydream Design Detection Developers Device Device Data Device Detection Device Intelligence Device Models Device property DeviceAtlas Disney dmexco DotNetNuke Download ebay Ericsson Event Facebook Firefox Foundation Framework Galaxy git repositories Google Google Analytics Google Daydream GPU GSMA Guess HAProxy Hash Trie HTC HTML5 HTTP HTTP Headers Huawei Infinix Ingeniux Internet usage iOS iOS 13 ipad iPadOS iPhone iPhone 11 Java Javascript Kentico LG Liferay LTE m.dot Memory Memory leak Meta Data Microsoft Mobile Mobile Analysis Mobile Analytics Mobile Devices Mobile Marketing Mixer Motorola Mozilla MWC MWC 2017 MWC16 Native Apps NET New Release News Nexus NFC NGINX Nokia OnePlus 5 Opera Operating System Oppo Optimisation OS Patent Performance PHP Press Release Price Band programmatic Publishers Python Redirection Research Responsive Images Responsive web design RESS Review RTB RWD Safari Samsung Scala ScientiaMobile SEO Server Server-side optimisation Seznam.cz Sitecore Smart TV Smartphone Smartwatches Sony Swedish Beers Tablet Tencent Testing Tips Tutorial Umbraco Update User Agent User-Agent Valgrind Varnish Varnish Cache Video Vodafone VoLTE Web Web Apps Web content management Webtrekk White Paper Widgets WiFi Windows WURFL Xiaomi Xperia ZTE