Benvenuti da Idea R | Vicenza - Romano di Lombardia (Bergamo)
Passa alla visualizzazione per i dispositivi mobili

      Idea R - Ce l'hai una vera strategia web? Tecnologie della Persuasione

Blog

Prendere uno smanettone del software degli anni 80, aggiungere un graphic designer di nuova generazione e allungare il tutto con uno studioso di strategie di marketing. Agitare energicamente e si ottiene il blog Idea R.

Change language... English

3 methods to detect mobile devices in ASP.NET

Pubblicato il 11/14/2012
Categorie: Siti Web
3 methods to detect mobile devices in ASP.NET

Redirecting mobile visitors to the mobile version of your site, is a common task.
How to detect them in ASP.NET?

Solution 1: the native ASP.NET method

The HttpBrowserCapabilitiesBase class exposes the IsMobileDevice property, that seems to be the solution to our problem.
Unfortunately the property returns highly inaccurate values, due to the very basic parsing of the browser user agent.

Solution 2: 51Degrees.mobi

This open source library does a good job, you can install it using NuGet or directly from the 51Degrees project site.
When you install this free component, it comes along with with a data file that contains all the detailed user agents information of all the mobile phones in the market.
The only thing you have to do, is to periodically refresh this data file.

From a programmers prospective, you can configure an automatic redirection directly using configuration parameters in your config file, or you can manually test the extended HttpBrowserCapabilitiesBase.IsMobileDevice property that now return accurate results.

But there's a problem!

If you have to distinguish between smartphones and tablets, with this solution you can't or better, you can if you switch to the premium paid version.

Until 51Degrees version 1.x this was possible because the component was based on the WURFL database (Wireless Universal Resource FiLe), but with the new 2.x versions this changed as stated inside the official documentation:

The detection element replaces the wurfl element used in previous versions of the Foundation (v0.x, v1.x). Due to licencing changes from ScientiaMobile, the 51Degrees.mobi Foundation no longer supports WURFL data and instead uses 51Degrees.mobi Device Data.

Solution 3: WURFL

WURFL (Wireless Universal Resource FiLe) is deployed by key Internet companies such as Facebook and Google and it is the most used solution worldwide.
It can be easily included in your project using NuGet, but it has not a drag'n'drop implementation like 51Degrees, you have to code a little bit.

It is a two step process. First load the devices database and cache it for future use (you can do this in the Application_Start method).

using WURFL;
using WURFL.Aspnet.Extensions.Config;

...

// Try to load the WURFL manager from the cache
IWURFLManager wurflManager = (IWURFLManager)HttpContext.Current.Cache[WurflManagerCacheKey];
if (wurflManager == null)
{
    ApplicationConfigurer configurer = new ApplicationConfigurer();
    wurflManager = WURFLManagerBuilder.Build(configurer);
    // ...store the data file in the cache
    HttpContext.Current.Cache.Insert(WurflManagerCacheKey, wurflManager, null,
        DateTime.Now.AddMonths(1), Cache.NoSlidingExpiration);
}

When a page is requested the second step is to verify if the visitor is using a smartphone:

using WURFL;
using WURFL.Aspnet.Extensions.Config;

...

IDevice device = wurflManager.GetDeviceForRequest(Request.UserAgent);
// ...do not consider tablets as mobile devices (they have a wider display)
Boolean smartphone = String.Compare(device.GetCapability("is_wireless_device"), true.ToString(), true) == 0 && String.Compare(device.GetCapability("is_tablet"), false.ToString(), true) == 0;
if (smartphone)
    // ...redirect to the mobile version of your site

Sei il lettore numero 11,382.

Commenti

Articolo precedente

Articolo precedente

jQuery: come lanciare e gestire gli eventi personalizzati

Articolo successivo

Caso di studio: branding Serendipity, campagna pubblicitaria (fase 6)

Articolo successivo

Torna all'inizio