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. There are also some useful links to commercial and open source projects for detecting mobile devices in that article.
- 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.
- Detect whether the current web site visitor is using a mobile device.
- 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 "UAgentInfo" Class
First, instantiate the UAgentInfo object, then call one of its functions. The device detection functions return booleans: true or false. It's as simple as that! Here's an example:
// 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.
protected override void OnInit(EventArgs e)
//Let's listen for an iPhone or iPod Touch
object page, MDetectPage.MDetectArgs args)
//Send the browser to a different page.
Response.Status = "301 Moved Permanently";
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.
- 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.
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#.