Detecting Mobile Devices Using ASP.NET and C#

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.

 

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;

 

Introducing MobileESP!

Since the original publication of this article in 2008, we've seen interest grow dramatically for this easy-to-use mobile detection library for web site publishers. As a result, we have spun this code library off into a separate free open source (Apace 2 license) code library called MobileESP. We've continuously expanded the capabilities of the PHP code library as new devices came out. So, for the latest information, including updates, bug fixes, new features, and more, please see the project web site: www.MobileESP.org. Here are a few quick links:

  • Get MobileESP: Download the latest version of the MobileESP code libraries! Learn more.
  • The MobileESP API: See the complete list of mobile detection functions which are largely consistent across supported web technologies (PHP, Java, APS.NET, Ruby, and JavaScript).
  • Live MobileESP API Demos: Want to see how the MobileESP APIs work with your device? Check out the PHP & JavaScript live demos!
  • Download Samples: There are several sets of sample web pages you can download to see how easy it is to integrate MobileESP into your web site. Learn more.
  • License Information: MobileESP is free to use, though donations are greatly appreciated and encouraged. For more information, please visit the Licensing page on the MobileESP web site.

 

Alternatives

MobileESP is easy to implement and generally great if you're primarily concerned about the class of device (e.g., smartphone or not, phone vs. tablet), or the mobile platform (e.g., iPhone or Symbian S60). If you need detailed device information or usage metrics, you may wish to check out WURFL (free!) or HandsetDetection.com.

 

About the Contributor

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

 

Support MobileESP!

If you find the MobileESP code library useful, please consider donating so we can purchase additional devices on our testing wishlist! Donations of $25-50 are greatly appreciated!

 

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.