\r\n

51Degrees Device Detection .NET  4.4

Device detection services for 51Degrees Pipeline

Cloud/Framework-Web/Default.aspx.cs

This example demonstrates how to use the cloud-based device detection API in a .NET Framework website.The source code for this example is available in full on GitHub.

Overview

The FiftyOne.Pipeline.Web package includes an IHttpModule implementation called 'PipelineModule', which replaces the default HttpCapabilitiesBase.BrowserCapabilitiesProvider with a 51Degrees version. This means that when values are requested (e.g. Request.Browser.IsMobileDevice), we can intercept that request and perform our own device detection based on the values in the HTTPRequest.

Configuration

By default, a 51Degrees.json file is used to supply the Pipeline configuration. For more details about the available options, check the relevant builder classes. For example, the CloudRequestEngineBuilder. The methods available on the builder are the same as those that will be available in the configuration file.

Note that you will need to create a 'resource key' using our configurator site in order to get this example to work. See our documentation for complete instructions. Once created, the key will need to be copied into this configuration file.

{
"PipelineOptions": {
"Elements": [
{
"BuilderName": "CloudRequestEngine",
// Obtain a resource key with the properties required to run this
// example for free: https://configure.51degrees.com/1QWJwHxl
"BuildParameters": {
"ResourceKey": "!!ENTER_YOUR_RESOURCE_KEY_HERE!!",
"CloudRequestOrigin": "51Degrees.example.com"
}
},
{
"BuilderName": "DeviceDetectionCloudEngine"
},
{
"BuilderName": "JavaScriptBuilderElement",
"BuildParameters": {
"Minify": true
}
}
],
"ClientSideEnabled": true
}
}

Web.config

The 51Degrees API mostly targets .NET Standard. This means you may get an error like:

CS0012: The type 'System.Object' is defined in an assembly that is not referenced. You must add
a reference to assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.

In this case, you will need to add the following section to your web.config:

<compilation debug="true" targetFramework="4.7.2">
<assemblies>
<add assembly="netstandard, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=cc7b13ffcd2ddd51"/>
</assemblies>
</compilation>

Load Assemblies

Any builders that are specified in configuration must also have their assemblies loaded into the AppDomain. This is handled in Global.asax:

<%@ Application Codebehind="Global.asax.cs" Inherits="Framework_Web.Global" Language="C#" %>

Results

This example includes a simple demonstration page that shows how to access different values from the results. For a complete list of the properties available, see our Configurator tool. This is also used to create the resource keys that are required when accessing our cloud service.

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Framework_Web._Default" %>
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<div class="jumbotron">
<h1>51Degrees</h1>
<p class="lead">51Degrees device detection can be used to deliver detailed insights into the device, operating system and browser being used to access your website.</p>
<p><a href="https://51degrees.com" class="btn btn-primary btn-lg">Learn more &raquo;</a></p>
</div>
<div class="row">
<div class="col-12">
<h2>ASP.NET integration</h2>
<p>This example demonstrates the use of the device detection API as part of an ASP.NET website:</p>
<p>Is the visitor using a mobile device? <%= Request.Browser.IsMobileDevice ? "Yes" : "No" %></p>
<p></p>
<p><strong>Browser Details:</strong> <%= Request.Browser["BrowserVendor"] %> <%= Request.Browser["BrowserName"] %> <%= Request.Browser["BrowserVersion"] %></p>
<p><strong>Evidence:</strong></p>
<% foreach (var evidence in ((FiftyOne.Pipeline.Web.Framework.Providers.PipelineCapabilities)Request.Browser).FlowData.GetEvidence().AsDictionary()) { %>
<p><%= evidence.Key %> - <%= evidence.Value %></p>
<% } %>
</div>
</div>
</asp:Content>
using System;
using System.Web.UI;
namespace Framework_Web
{
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
}
}