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!

21 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..

  14. Amaresh said,

    Wrote on August 30, 2010 @ 7:07 am

    The latest capabilities list which is available in WURFL is read using 51degrees.mobi Foundation API which detects all mobile devices. It is a .NET open source available here http://51degrees.codeplex.com/releases.

    It easily detects if the request is coming from any mobile device and redirects user to mobile landing page. It has some user friendly samples to try out and a video tutorial http://51degrees.codeplex.com/wikipage?title=EnhanceTutorial

  15. Esben Rasmussen said,

    Wrote on November 14, 2010 @ 11:12 am

    I have used http://www.deviceatlas.com i several projects and i really workes well.
    Besides identifying if the visitor is a mobile device, you can scale for screen size, java version and XHTML version and more.

  16. Mehrdad Zandi said,

    Wrote on November 24, 2010 @ 3:56 am

    Thanks very much for a nice code for detecting
    different devices.
    I want to know how to detect the follwoing devices:
    1- SonyEricsson Xpreia 10?
    2- HP ipaq?

    when i looking the code i see the follwoing:
    userAgent.Contains(“opera mini”) or
    userAgent.Contains(“windows ce”) ?
    what these means?
    what i undrestand that a device with opera min or windows ce. am i correct?
    But different devices can have the same operationg system.

    If you want exactly detect unique device and adapt your webstie to this device you should give the
    unique id for the device?
    thanks in advance.
    /Mehrdad

  17. Ryan said,

    Wrote on January 20, 2011 @ 11:04 am

    I had to add “windows phone” for it to pick up Windows Mobile 6.5 Internet Explorer.

  18. praveen said,

    Wrote on March 1, 2011 @ 4:16 pm

    Is there a way to get a unique device Id from various mobile device.

  19. Mobile device detection | Pictivate Blog said,

    Wrote on May 16, 2011 @ 9:01 pm

    [...] Detecting mobile device user agents in ASP.NET [...]

  20. santosh said,

    Wrote on July 5, 2011 @ 4:18 pm

    Hello,

    What about Android devices then? Please let me know.

    Thank you,

  21. Maria said,

    Wrote on October 25, 2011 @ 2:10 pm

    Where does this code go…?

Comment RSS · TrackBack URI

Leave a Comment

Name: (Required)

E-mail: (Required)

Website:

Comment: