Detecting mobile device user agents in ASP.NET

If you’re developing a mobile version of your website, usability should be one of your top priorities. Most sites will detect if you’re using a mobile device, and automatically redirect you to the mobile version. I’m going to show you how to do this in ASP.NET.

Detecting a mobile device based on user agent

The first issue I ran into was coming up with a reliable way to determine if the device is a mobile device based on its user agent. My first urge was to use the detection built into ASP.NET:

Request.Browser.IsMobileDevice

I immediately lost any trust in this property after it failed to correctly identify Opera Mobile on my Windows Mobile phone. I suspect something similar will happen with the iPhone.

After browsing the dozens of common mobile device user agents, I came up with this method:

public static bool IsMobile(string userAgent)
{
	userAgent = userAgent.ToLower();

	return userAgent.Contains("iphone") |
		 userAgent.Contains("ppc") |
		 userAgent.Contains("windows ce") |
		 userAgent.Contains("blackberry") |
		 userAgent.Contains("opera mini") |
		 userAgent.Contains("mobile") |
		 userAgent.Contains("palm") |
		 userAgent.Contains("portable");
}

I realize this method isn’t perfect, but I believe it will correctly identify 99% of the mobile devices out there. You simply pass in the user agent, which is easy to get:

Request.UserAgent

If you want to be a little more precise, you can come up with your own method by looking at the list of mobile agents, or you can use an updated list of mobile devices with detailed specifications. You can find that type of list here.

Have a better way to do it? Have a common device that my method doesn’t work for? Please let me know in the comments!

Kick It!

13 Comments so far »

  1. ryan whippo said,

    Wrote on March 17, 2009 @ 8:12 am

    Spot on! Great job.

  2. spottedplates said,

    Wrote on March 19, 2009 @ 2:04 am

    Works great. Exactly what I was looking for. Thank you for taking the time to do this.

  3. sj said,

    Wrote on April 13, 2009 @ 8:42 pm

    Which is the best place to write this code? Session_Start event?

  4. admin said,

    Wrote on April 14, 2009 @ 7:07 am

    You could put it anywhere that the context is available. Use it where you think it’s convenient.

  5. Patrik said,

    Wrote on June 2, 2009 @ 12:58 pm

    I had to remove “PPC” since Firefox 3.x on Mac includes PPC. Otherwise it works perfectly!

  6. vivek said,

    Wrote on July 20, 2009 @ 12:43 am

    Thanxs for sharing :) its very useful

  7. JB said,

    Wrote on July 20, 2009 @ 12:19 pm

    This wouldn’t detect my Nokia device, add “SymbianOS” and it should pick up a lot more devices.

    Good work!

  8. ms said,

    Wrote on July 21, 2009 @ 5:18 pm

    thanks for the nice code , and also keep it up

    Good work ..

  9. JB said,

    Wrote on July 23, 2009 @ 5:35 pm

    I just got a false positive from a Mac user using Safari 3.2 – the string “ppc” is the offender, here is the user agent string:

    HTTP/1.1 Mozilla/5.0+(Macintosh;+U;+PPC+Mac+OS+X+10_4_11;+en)+AppleWebKit/525.27.1+(KHTML,+like+Gecko)+Version/3.2.1+Safari/525.27.1

    Don’t use “ppc” people – also, if you want to be tight see: http://user-agent-string.info/list-of-ua

  10. Amit Patel said,

    Wrote on July 31, 2009 @ 2:15 am

    You can easily determine mobile device and its details using open source API http://www.51degrees.mobi/Products/NETMobileAPI

    This API gives detailed information of the device making the request and also has facility to redirect user automatically to mobile specific page when request in coming from mobile device.

    I found it very useful and easy to implement in my mobile website developments.

    Let me know if you face any issues in using.

    Thanks

  11. Sameer SK said,

    Wrote on April 12, 2010 @ 4:46 am

    Superb Solution..!Thanks!

  12. David said,

    Wrote on May 31, 2010 @ 1:14 am

    Hi, I’m just getting started with mobile development. When requesting the user agent does it need to be on the default.cs page or can it be put in an gobal file?

    Thanks

  13. noklpopl said,

    Wrote on July 18, 2010 @ 7:49 am

    hope this works..

Comment RSS · TrackBack URI

Leave a Comment

Name: (Required)

E-mail: (Required)

Website:

Comment: