This example shows how to integrate the Pipeline API with a device detection engine into an ASP.NET Core web app.The source code for this example is available in full on GitHub.
This example can be configured to use the 51Degrees cloud service or a local data file. If you don't already have data file you can obtain one from the device-detection-data GitHub repository.
@model FiftyOne.DeviceDetection.IDeviceData
@{
ViewData["Title"] = "Device detection example";
var hardwareVendor = Model.HardwareVendor;
var hardwareName = Model.HardwareName;
var deviceType = Model.DeviceType;
var platformVendor = Model.PlatformVendor;
var platformName = Model.PlatformName;
var platformVersion = Model.PlatformVersion;
var browserVendor = Model.BrowserVendor;
var browserName = Model.BrowserName;
var browserVersion = Model.BrowserVersion;
var screenWidth = Model.ScreenPixelsWidth;
var screenHeight = Model.ScreenPixelsHeight;
}
<h2>Example</h2>
<div id="content">
<p>
The following values are determined by sever-side device detection
on the first request:
</p>
<p>
<b>Hardware Vendor:</b> @(hardwareVendor.HasValue ? hardwareVendor.Value : $"Unknown ({hardwareVendor.NoValueMessage})")<br />
<b>Hardware Name:</b> @(hardwareName.HasValue ? string.Join(", ", hardwareName.Value) : $"Unknown ({hardwareName.NoValueMessage})")<br />
<b>Device Type:</b> @(deviceType.HasValue ? deviceType.Value : $"Unknown ({deviceType.NoValueMessage})")<br />
<b>Platform Vendor:</b> @(platformVendor.HasValue ? platformVendor.Value : $"Unknown ({platformVendor.NoValueMessage})")<br />
<b>Platform Name:</b> @(platformName.HasValue ? platformName.Value : $"Unknown ({platformName.NoValueMessage})")<br />
<b>Platform Version:</b> @(platformVersion.HasValue ? platformVersion.Value : $"Unknown ({platformVersion.NoValueMessage})")<br />
<b>Browser Vendor:</b> @(browserVendor.HasValue ? browserVendor.Value : $"Unknown ({browserVendor.NoValueMessage})")<br />
<b>Browser Name:</b> @(browserName.HasValue ? browserName.Value : $"Unknown ({browserName.NoValueMessage})")<br />
<b>Browser Version:</b> @(browserVersion.HasValue ? browserVersion.Value : $"Unknown ({browserVersion.NoValueMessage})")<br />
<b>Screen width (pixels):</b> @(screenWidth.HasValue ? screenWidth.Value.ToString() : $"Unknown ({screenWidth.NoValueMessage})")<br />
<b>Screen height (pixels):</b> @(screenHeight.HasValue ? screenHeight.Value.ToString() : $"Unknown ({screenHeight.NoValueMessage})")
</p>
<p>
The information shown below is determined from JavaScript running on the client-side that is able to obtain additional evidence. If no additional information appears then it may indicate an external problem such as JavaScript being disabled in your browser.
</p>
<p>
Note that the 'Hardware Name' field is intended to illustrate detection of Apple device models as this cannot be determined server-side. This can be tested to some extent using most emulators such as those in the 'developer tools' menu in Google Chrome. However, using real devices will result in more precise model numbers.
</p>
</div>
@await Component.InvokeAsync("FiftyOneJS")
<script>
// This function will fire when the JSON data object is updated
// with information from the server.
// The sequence is:
// 1. Response contains JavaScript properties 'ScreenPixelsHeightJavaScript', 'ScreenPixelWidthJavaScript' and 'JavaScriptHardwareProfile'. These are executed on the client.
// 2. This triggers another call to the webserver that includes the evidence gathered by these JavaScript properties.
// 3. The web server responds with new JSON data that contains the updated property values based on the new evidence.
// 4. The JavaScript integrates the new JSON data and fires the 'complete' callback below.
window.onload = function () {
fod.complete(function (data) {
let fieldValues = [];
fieldValues.push([ "Hardware Name: ", data.device.hardwarename.join(", ") ]);
fieldValues.push([ "Screen width (pixels): ", data.device.screenpixelswidth ]);
fieldValues.push([ "Screen height (pixels): ", data.device.screenpixelsheight ]);
displayValues(fieldValues);
});
function displayValues(fieldValues) {
var para = document.createElement("p");
fieldValues.forEach(function (entry) {
var br = document.createElement("br");
var bold = document.createElement('b');
var fieldname = document.createTextNode(entry[0]);
var fieldvalue = document.createTextNode(entry[1]);
bold.appendChild(fieldname);
para.appendChild(bold);
para.appendChild(fieldvalue);
para.appendChild(br);
});
var element = document.getElementById("content");
element.appendChild(para);
}
}
</script>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using FiftyOne.Pipeline.Core.FlowElements;
using FiftyOne.Pipeline.Engines.Services;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddTransient<HttpClient>();
services.AddSingleton<IFileWrapper, FileWrapper>();
services.AddSingleton<IFileSystem, RealFileSystem>();
services.AddSingleton<Func<TimerCallback, object, TimeSpan, Timer>>(
(callback, state, dueTime) =>
{
return new Timer(callback, state, (long)dueTime.TotalMilliseconds, Timeout.Infinite);
});
services.AddSingleton<IDataUpdateService, DataUpdateService>();
services.AddSingleton<DeviceDetectionHashEngineBuilder>();
services.AddFiftyOne(Configuration);
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
app.UseFiftyOne();
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseCookiePolicy();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
}
}