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

Trie matching

What is TRIE matching?

Trie is the fastest device detection solution that 51Degrees provides at the expense of the data file size and lack of auto updates. The size of the uncompressed data file is close to 1Gb for Premium and Enterprise files. Learn how Trie matching works.

Trie in the .NET environment

Currently Trie for .NET relies on reading the data directly from the data file to perform device detection.

Offline Projects

To perform device detection using the Trie provider:

  1. Create a TrieProvider object by invoking the create method of the TrieFactory.
  2. Call the GetDeviceIndex function and supply it with the User Agent you want to match. The function will return device index as an integer.
  3. Call the GetPropertyValue function and supply it with the device index from step 2 and the name of the property you want to get, i.e. "IsMobile".

using FiftyOne.Foundation.Mobile.Detection;
using FiftyOne.Foundation.Mobile.Detection.Factories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TrieOfflineMatching
{
    class Program
    {
        static void Main(string[] args)
        {
            //Set up provider.
            TrieProvider trieProvider;
            trieProvider = TrieFactory.Create("C:\\Path\\To\\51Degrees.trie");

            //Perform detection.
            int deviceIndex = trieProvider.GetDeviceIndex("User Agent String");
            string property = trieProvider.GetPropertyValue(deviceIndex, "IsMobile");

            Console.WriteLine(property);
            Console.ReadLine();
        }
    }
}

Websites and Web Projects

By default the DLL is supplied with the Pattern provider that is controlled through the 51Degrees.config file, If you wish to use Trie detection you will need to set the enabled option to false in the detection section of the 51Degrees.config to avoid performing detection with the built in Pattern method and comment out the redirect section.

If you do not have the Global.asax file in your project you will need to create one. You will then need to create a public static TrieProvider object and instantiate it upon the ApplicationStart. Your Global.asax should look something like this:

using FiftyOne.Foundation.Mobile.Detection;
using FiftyOne.Foundation.Mobile.Detection.Factories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;

namespace WebApplication5
{
    public class Global : System.Web.HttpApplication
    {
        public static TrieProvider trieProivder;

        protected void Application_Start(object sender, EventArgs e)
        {
            trieProivder = TrieFactory.Create("C:\\Path\\To\\51Degrees.trie");
        }

        protected void Session_Start(object sender, EventArgs e)
        {

        }

        ...

        protected void Application_End(object sender, EventArgs e)
        {

        }
    }
}

The idea is that the detection will take place upon the Page_Load. For example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication5
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //Get device index and get the IsMobile property for that index.
            int index = Global.trieProivder.GetDeviceIndex(this.Request.UserAgent);
            string isMobile = Global.trieProivder.GetPropertyValue(index, "IsMobile");
            //Apply some logic.
            if (isMobile.ToLower().Equals("true")) {
                lbl1.Text = "Mobile";
            } else {
                lbl1.Text = "Not Mobile";
            }
        }
    }
}

Once the device has been detected you can then apply some logic. In this example the text of the asp label changes to either "True" or "False" depending on the outcome, but you can do other things like redirect or change the content being added.

MVC projects follow similar logic. You will need a Global.asax to instantiate a TrieProvider at application start. The detection and logic are then executed in the Controller.

Trie data file updates

Currently the .NET API does not support automatic updates for the Trie data files due to the size of the data files. The 51Degrees update server is configured to supply Trie data files if requested so you can implement the update logic yourself. For more information on data updates please see: Automatic Updates section of the documentation.