Detecting Mobile Devices Using ASP.NET and C#

New: November 2009

Thanks to the magical powers of the Internet, a kind fan of the PHP scripts for detecting mobile devices ported the code to ASP.NET and C#. So for those folks using ASP.NET, here is a fantastic new option. And of course -- it's free!

For more detailed information about the caveats of attempting to detect mobile devices based on User Agent strings, please see our article on PHP scripting. There are also some useful links to commercial and open source projects for detecting mobile devices in that article.

Quick apologies: I'm not as versed in ASP.NET as JavaScript and PHP, so any errors introduced in the code are mine. Brad, the gentleman who ported the PHP code to ASP.NET, did a great job! Since he sent the code to me, I've made a few minor modifications, including Palm WebOS support, some additional OnDetect events, and some additional commenting. Please accept my apologies in advance for any errors. And if you find any, please let me know!

 

Summary

As with PHP and JavaScript code, the basic concept is to do the following:

  1. Create a custom Web Page class which inherits from the mobile-detecting base class, MDetectPage, described below. All of the mobile dection methods and events are built in.
  2. Detect whether the current web site visitor is using a mobile device.
  3. If yes, redirect to a mobile-optimized page. (Or similar type of custom action for mobile devices.)

 

Introducing the "MDetectPage" Class

The ASP.NET class called "MDetectPage" inherits from System.Web.UI.Page and encapsulates all of the logic for detecting mobile devices. The page adds an elegant delegates and events model for detecting mobile devices that is consistent with the ASP.NET programming paradigm.

As a result, the easiest thing to do is to simply sub-class the MDetectPage and listen for the "OnDetectXXX()" event to fire for the device type that you're interested in. For example, if you'd like to send iPhones and Android devices to your special touch-optized mobile site, listen for the OnDetectTierIphone event to fire. Or, if you only have one mobile site that serves pretty much any type of device, listen instead for the OnDetectMobileQuick event.

Note that this code is easy to use and its API is highly modularized so that you can detect broad classes of devices (such as smartphones or WAP/WMP-capable devices) and specific platforms (such as the iPhone/iPod Touch, Android or BlackBerry). Feel free to further optimize the code for your own uses and fill in any of the missing OnDetect events.

Using the "MDetectPage" Class

A practical usage of this code would be to redirect the mobile device automatically so that it is sent to a mobile-friendly web page. For example, a PC-oriented page might have heavy Flash usage, which is typically unsupported on mobile devices.


//**************************
// ASP.NET Service Page name: MDetect_Test.aspx
// This page inherits from the MDetectPage.
// This test page will listen for an iPhone or
// iPod Touch device and redirect it to a special page. 
public partial class MDetect_Test : MDetectPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //Call this method to triger the detection methods.
        this.FireEvents();
    }

    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);

        //Let's listen for an iPhone or iPod Touch 
        OnDetectDetectIPhoneOrIpod += 
           new DetectIPhoneOrIpodHandler(
           MDetect_Test_OnDetectDetectIPhoneOrIpod);
    }

    void MDetect_Test_OnDetectDetectIPhoneOrIpod(
       object page, MDetectPage.MDetectArgs args)
    {
        //Send the browser to a different page. 
        Response.Redirect("http://www.mycompany.com/iphone");
    }
}


Let's look at some specific features of the API.

Detect iPhone & iPod Touch

Use the following code to detect whether the device viewing the page is an iPhone and/or iPod Touch. And don't forget: iPod Touches are devices, too!

Why detect this browser? Mobile Safari is based on WebKit and can render most desktop-targeted web content very well (generally excluding heavy AJAX pages). Don't send users to barebones WAP/WML pages! Instead, give iPhone users regular desktop pages, iPhone-optimized content or nicely formatted mobile-optimized content.


/**
 * Detects if the current device is an iPhone or iPod Touch.
 * In most cases, you'll want to use this function.
 */
public delegate void DetectIPhoneOrIpodHandler(
   object page, MDetectArgs args);
public event DetectIPhoneOrIpodHandler OnDetectDetectIPhoneOrIpod;

/**
 * Detects if the current device is an iPhone.
 */
public delegate void DetectIphoneHandler(object page, MDetectArgs args);
public event DetectIphoneHandler OnDetectIphone;

/**
 * Detects if the current device is an iPod Touch.
 */
public delegate void DetectIpodHandler(object page, MDetectArgs args);
public event DetectIpodHandler OnDetectIpod;

 

Detect Android Devices

Besides the iPhone, the only other mobile devices in recent times to receive as much attention, both from geeky fanboys and the general public, have been the new devices running Android. Android is a brand new operating system for mobile phones created by Google. Android is a smartphone-class OS with an advanced browser similar to the iPhone's in capabilities.

Why detect this browser? The native Android browser is extremely capable and also based on WebKit, which underlies the iPhone's Mobile Safari & the S60 browser. Generally, if you have web content optimized for the iPhone, also make this content available for Android devices. Additional manufacturers began releasing loads of new Android devices in mid-2009.


/**
 * Detects if the current device is an Android OS-based device.
 */
public delegate void DetectAndroidHandler(
   object page, MDetectArgs args);
public event DetectAndroidHandler OnDetectAndroid;

 

Detect Symbian S60 Smartphones

The most popular smartphone platform in the world is Symbian S60. Used primarily by Nokia and a few other manufacturers, S60 features a very capable browser.

Why detect this browser? The S60 Open Source Browser is based on WebKit and can render most desktop-targeted web content very well (generally excluding heavy AJAX pages). Don't send users to barebones WAP/WML pages! Instead, give S60 users regular desktop pages or nicely formatted mobile-optimized content.


/**
 * Detects if the current browser is the S60 Open Source Browser.
 */
public delegate void DetectS60OssBrowserHandler(
   object page, MDetectArgs args);
public event DetectS60OssBrowserHandler OnDetectS60OssBrowser;

/**
 * Detects if the current device is any Symbian OS-based device,
 *   including older S60, Series 70, Series 80, Series 90, and UIQ, 
 *   or other browsers running on these devices.
 */
public delegate void DetectSymbianOSHandler(
   object page, MDetectArgs args);
public event DetectSymbianOSHandler OnDetectSymbianOS;

 

Detect Windows Mobile Devices

Devices running Windows Mobile are fairly popular in the U.S., especially among business users. This code detects both the non-touch screen (Standard/Smartphone) and touch screen (Professional/PocketPC) types of devices.

Why detect this browser? Pocket Internet Explorer and Opera for Windows Mobile are moderately capable mobile browsers. Don't send users to barebones WAP/WML pages! Instead, give these users either modest desktop pages (avoid AJAX and complicated CSS) or nicely formatted mobile-optimized content. (The latter is probably the better choice.)


/**
 * Detects if the current browser is a Windows Mobile device.
 */
public delegate void DetectWindowsMobileHandler(
   object page, MDetectArgs args);
public event DetectWindowsMobileHandler OnDetectWindowsMobile;

 

Detect BlackBerry Devices

The most popular smartphone platform in the United States. (Still beats the iPhone in sales!) Unfortunately, the BlackBerry browser isn't great.

Why detect this browser? Although the browser isn't as capable as the WebKit ones noted above, it can display nicely (but minimally!)-formatted mobile-optimized content. At least when the browser is set to HTML mode! Otherwise, in WML-only mode, it should be sent the barebones WAP/WML pages.


/**
 * Detects if the current browser is on a BlackBerry of some sort.
 */
public delegate void DetectBlackBerryHandler(
   object page, MDetectArgs args);
public event DetectBlackBerryHandler OnDetectBlackBerry;

 

Detect PalmOS and WebOS Devices

Last but not least in the smartphone category are devices from Palm. Palm has a new Linux-based operating system for its touchscreen phones launched in mid-2009 call "WebOS". The Pre and the Pixi are the first two WebOS devices. In addition, there is a large installed base of old PalmOS devices.

Why detect this browser? The new WebOS browser shares the same WebKit core as the iPhone's and Android's browsers, though it appears slightly less capable than they are. Still, if a web site is optimized for the iPhone and Android, it may run well on WebOS devices, too. As for older PalmOS devices, I recommend sending them minimally formatted mobile-optimized content.


/**
 * Detects if the current browser is on a newer Palm device
 * running the new WebOS.
 */
public delegate void DetectPalmWebOSHandler(
   object page, MDetectArgs args);
public event DetectPalmWebOSHandler OnDetectPalmWebOS;

/**
 * Detects if the current browser is on an older
 * PalmOS device.
 */
public delegate void DetectPalmOSHandler(
   object page, MDetectArgs args);
public event DetectPalmOSHandler OnDetectPalmOS;

 

Additional API Details

Now that we've seen the general pattern for how to use our MDetectPage class works, let's see a few more types of devices it can detect. In the interest of space, we won't show the code for each. (But you can download it below!)

  • Detect any smartphone-class device: OnDetectSmartphone
  • Detect any iPhone-tier touchscreen device, including Android and Palm WebOS: OnDetectTierIphone
  • Detect any browser based on WebKit: OnDetectWebkit
  • Quick check to detect any mobile device. Ought to detect most modern mass market phones as well as smartphones: OnDetectMobileQuick
  • This device's browser supports WAP/WML content: OnDetectWapWml
  • This device is BREW-powered: OnDetectBrewDevice
  • This is a Danger Hiptop: OnDetectDangerHiptop

 

Enjoy the fun at home!

Download the ASP.NET code, including a sample web page, from our new Google Code page!

The most important caveat is that you should thoroughly test the code for yourself based on your own needs and expectations -- and always using your high-priority target devices!

If you find this code useful, please consider donating so we can purchase additional devices on our testing wishlist!


About the Contributor

Our gratitude to Brad Rigg, who wrote the initial port of the PHP code to ASP.NET and C#.

 

License Info & Costs

We have moved this code library to a Google Code hosting project and changed the license to an Apache License 2.0, which better fits a free, open source project like this. We've also rechristened this project "MobileESP." For more info, please visit the new MobileESP site: www.mobileESP.org


MobileESP Project

Learn more about the new MobileESP project!

We've spun off our mobile device detection code into a FREE open source project called MobileESP! Learn more about it on the new project web site.

Please Donate

If you like and use this code, please consider donating. Any amount is appreciated!

(Please donate -- help us purchase more devices on our testing wishlist!)

About Us

Hand Interactive offers user experience, usability, and business strategy expertise. We're passionate about crafting engaging experiences for mobile, desktop, and web users.

 
Contact us for more information.