Check out the latest documentation.

Introduction

This getting started guide covers 3 common scenarios.

  1. A web site using .NET version 4 and NuGet package manager. This is the easiest installation and is recommended.
  2. A web site using .NET version 3.5 or one that does not have access to NuGet package manager.
  3. A project that is not a web site.

Choose the scenario which is most relevant to your project requirements.

In all scenarios three different device data sets can be used for device matching. By default the free Lite data set will be used and is shown in these getting started guides. Compare the differences between the different data sets and download free evaluations of paid for data to access enhanced features, more device properties and automatic updates. 

1. .NET v4 Web Site & NuGet

Complete the following four steps to integrate mobile device detection into your Visual Studio 2012 .NET web application.

Step 1 - Search for 51Degrees in the Nuget package manager.

Step 2 - Press Install. Nuget will automatically add the necessary files and references to your project.

Step 3 - You can now use 51Degrees properties with the Request.Browser object using the following example code. An example web page will be available in the Mobile folder of the web site. If MVC is the target page type then the WebMatrix version of 51Degrees on NuGet will install an MVC Razor example web page. Access the web site from a mobile device to be redirected to the example web page.

Step 4 - If required reference the FiftyOne.Foundation.dll assembly from within Visual Studio to access the WebProvider as shown in the 2nd and 3rd lines of the OnInit example method below.

										
										using
										
										
										System
										
									;

										
										using
										
										
										FiftyOne.Foundation.Mobile.Detection
										
									;


										
										namespace
										
										
										Detector
										
									
{
    
										
										public
										
										
										partial
										
										
										class
										
										
										Home
										
									 : System.Web.UI.Page
    {
        
										
										protected
										
										
										override
										
										
										void
										
										
										OnInit
										
									(EventArgs e)
        {
            
										
										// properties can be quickly found through the Request.Browser object
										
										
										bool
										
									 isMobile = Request.Browser[
										
										"IsMobile"
										
									] == 
										
										"True"
										
									;

            
										
										// the provider can can be used to list all device, properties and more
										
									
            Provider provider = WebProvider.ActiveProvider;

            
										
										// it can also be used to get device information directly if
										
										
										// Request.Browser is unavailable
										
									
            Match match = provider.Match(Request.UserAgent);
        }
    }
}

									

2. .NET 3.5 Web Site or No NuGet

All the files needed to add 51Degrees to an existing project (including one that is not a web site) are also available from Codeplex by downloading the zip file titled 51Degrees DLL Website Enhancer . The following files are included:

FileName Description
NET4/bin/FiftyOne.Foundation.dll .NET v4 assembly for 51Degrees.
NET35/bin/FiftyOne.Foundation.dll .NET v3.5 assembly for 51Degrees
51Degrees.config Configuration file for 51Degrees
M.aspx An example web forms page to test functionality

Place the 51Degrees.config file in either the App_Data folder or the root folder for the web site.

The 51Degrees.config file contains logic to redirect a mobile device to the M.aspx also provided in the zip file. Place the M.aspx file in the root folder of the web site.

Depending on the version of .NET being used follow one of the next two steps.

.NET 4 or Later

Copy the DLL from NET4 directory in to your project's bin directory.

Web sites running .NET v4 enable assemblies to register themselves automatically when the web site starts. Therefore they do not need to be referenced in the web.config file. If the detection functionality fails to work it's possible the website has been configured to prevent the automatic registration of assemblies. If this happens continue to the instructions for .NET 3.5 which explain how to explicitly register the assembly with the website.

.NET 3.5

The following entries need to be added to your existing web.config file for mobile device detection and redirection to be enabled. The comments in green do not need to be inserted but have been included for easy reference.

Where a section already exists with the primary element name, configSections or system.web/httpModules insert the section highlighted in grey before the closing tag.

For IIS 6.0 or IIS7 in Classic Mode

										
										<system.web>
										
										
										<!-- IIS 6.0 & Visual Studio - Registers a module that is used to detect any new requests to the web site. Without this module mobile detection and redirection won't work.-->
										
										
										<httpModules>
										
										
										<add
										
										
										name=
										
										
										"Detector"
										
										
										type=
										
										
										"FiftyOne.Foundation.Mobile.Detection.DetectorModule, FiftyOne.Foundation"
										
										
										/>
										
										
										</httpModules>
										
										
										</system.web>
										
									

 

For IIS 7.0 and above in Integrated Mode

										
										<system.webServer>
										
										
										<!-- IIS 7.X - Registers a module that is used to detect any new requests to the web site. Without this module mobile detection and redirection won't work.-->
										
										
										<modules>
										
										
										<remove
										
										
										name=
										
										
										"Detector"
										
										
										/>
										
										
										<add
										
										
										name=
										
										
										"Detector"
										
										
										type=
										
										
										"FiftyOne.Foundation.Mobile.Detection.DetectorModule, FiftyOne.Foundation"
										
										
										/>
										
										
										</modules>
										
										
										</system.webServer>
										
									

Confirming Deployment

Access the web site from a mobile device, or emulator, and confirm that the M.aspx page is being deployed.

If you require more than the simple redirection service and properties exposed via the Request.Browser class reference the assembly from within the Visual Studio project. The sample code shown for NuGet at the top of this page can be used to interface with 51Degrees.

For more information on the configuration options available for web applications see the Web Applications documentation.

3. Non Web Site Projects

Start by referencing the FiftyOne.Foundation.dll assembly contained in the 51Degrees DLL Website Enhancer explained in the above section from within the Visual Studio project choosing the relevant .NET version.

Important : Ensure the Target Framework of your project is NOT set to the Client Profile version of .NET.

Download from Codeplex the file 51Degrees Lite Device Data and decompress it.

The following example code shows how to create a data set and provider for the purposes of device detection using an example User-Agent string.

										
										// Create a data set for use with the device detection provider.
										
										
										using
										
									 (
										
										var
										
									 dataSet = FiftyOne.Foundation.Mobile.Detection.Factories.StreamFactory.Create(
		
										
										"[PATH TO YOUR DECOMPRESSED DATA FILE]"
										
									))
{
	
										
										// Create a provider to interface with the data set.
										
										
										var
										
									 provider = 
										
										new
										
									 FiftyOne.Foundation.Mobile.Detection.Provider(dataSet);

	
										
										// Perform device detection on a given user agent.
										
										
										var
										
									 result = provider.Match(
										
										"Mozilla/5.0 (Linux; Android 4.0.3; HTC One X Build/IML74K) "
										
									 +
		
										
										"AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.133 Mobile Safari/535.19"
										
									);

	
										
										// Display the results of the detection.
										
									
	Console.WriteLine(
										
										"IsMobile: {0}"
										
									, result[
										
										"IsMobile"
										
									]);
	Console.ReadKey();
}

									

For more information on using 51Degrees in non web project see the Offline Applications documentation.

3.1 Example of an Offline Command-Line C# Application

The following example implements the most basic analysis of User-Agents log file. For the purpose of this program both the 51Degrees-Lite.dat file and the text file containing User-Agent strings are located in C:/data/ directory. The User-Agents text file contains one User-Agent per line.

Please note, you can also use the million.csv file that is available for download from the 51Degrees website as the User-Agent data/log file (not the 51Degrees-Lite.dat).

  1. Start by creating a new C# - Windows - Console Application project.
  2. Now, copy the correct DLL to your bin directory. This will either be NET 4 or NET 3.5 DLL. If you are not sure please read section 2 of "Getting Started" section of .NET.
  3. If a reference to 51Degrees DLL does not appear add it manually by right-clicking on "References" in Solution Explorer and choosing "Add Reference" as shown below:
  4. Solution Explorer print screen

  5. In the reference manager window that opened select the "Browse" button at the bottom of the window. A file explorer will open. Navigate to the bin folder containing the 51Degrees DLL and select the 51Degrees DLL. Click "Add".
  6. Choosing the 51Degrees DLL.

  7. Now you are ready to use the 51Degrees DLL. The snippet below demonstrates how a text file with User-Agent strings can be opened, read in line by line with each line containing a User-Agent. Each line would be matched and the results displayed.

										
										using
										
										
										System
										
									;

										
										using
										
										
										System.Collections.Generic
										
									;

										
										using
										
										
										System.Linq
										
									;

										
										using
										
										
										System.Text
										
									;

										
										using
										
										
										System.Threading.Tasks
										
									;

										
										using
										
										
										FiftyOne.Foundation.Mobile.Detection
										
									;

										
										using
										
										
										FiftyOne.Foundation.Mobile.Detection.Factories
										
									;

										
										using
										
										
										System.IO
										
									;


										
										namespace
										
										
										ConsoleApplication2
										
									
{
    
										
										class
										
										
										Program
										
									
    {
        
										
										/* Create a 51Degrees provider using data file. File location specified by path. */
										
										
										static
										
									 Provider 
										
										CreateProvider
										
									(String path)
        {
            
										
										// create a provider
										
									
            Provider provider = 
										
										new
										
									 Provider(StreamFactory.Create(path));
            
										
										return
										
									 provider;
        }

        
										
										static
										
										
										void
										
										
										Main
										
									(
										
										string
										
									[] args)
        {
            
										
										//Set up file path variables.
										
									
            String path_to_agents_file = 
										
										"C://data//agents.txt"
										
									;
            String path_to_data_file = 
										
										"C://data//51Degrees-LiteV3.dat"
										
									;

            
										
										//Set up 51Degrees dataset, provider and match.
										
										
										var
										
									 dataSet = FiftyOne.Foundation.Mobile.Detection.Factories.StreamFactory.Create(path_to_data_file);
            Provider provider = CreateProvider(path_to_data_file);
            Match match;

            
										
										//Variable that holds user agent string from user agents file.
										
									
            String temp_agent;

            
										
										//To count how many user agent strings have been read.
										
										
										int
										
									 counter = 
										
										1
										
									;

            
										
										//Read in data from file.
										
										
										using
										
									 (System.IO.StreamReader file = 
										
										new
										
									 System.IO.StreamReader(path_to_agents_file))
            {

                
										
										while
										
									 ((temp_agent = file.ReadLine()) != 
										
										null
										
									)
                {
                    Console.WriteLine(
										
										"Agent: {0} - {1}"
										
									,counter, temp_agent);
                    match = provider.Match(temp_agent);
                    Console.WriteLine(
										
										"IsMobile: {0}"
										
									, match.Results[
										
										"IsMobile"
										
									]);
                    Console.WriteLine(
										
										"Screen Width: {0}"
										
									, match.Results[
										
										"ScreenPixelsWidth"
										
									]);
                    Console.WriteLine(
										
										"Screen Height: {0} \n"
										
									, match.Results[
										
										"ScreenPixelsHeight"
										
									]);
                    counter++;
                }
            }
            Console.ReadKey();
        }

    }
}

									

 

The above code should produce output similar to the one shown below:

Output of console application on c# using the 51Degrees DLL.

Further Reading