ASP.NET Mobile Device Detection

With the increasing use of mobile devices and mobile applications webmasters want to make their websites mobile friendly more than ever.

If you are a .NET developer like me and want to detect mobile devices this one is for you.

1- ASP.NET Support ?!? for Mobile Device Detection:

The simple if condition below can be used in Page_Load event of your default page to redirect your mobile visitors:

if (Request.Browser.IsMobileDevice)


When I tried the code with my iphone I was very happy to see it work, but just a minute after that tried browsing with an HTC and bumm… It failed to detect the mobile device hence no redirection.

Apparently inherent .NET mobile detection support is no where near detection.

So we should try another way. Javascript may be?

2- Mobile Device Detection Using JavaScript:

2a- Get the userAgent variable from navigator object

var u_agent = navigator.userAgent.toLowerCase();

Then detect a mobile device for example iphone as below

function isIphone() 

    if (u_agent.indexOf("iphone") > -1) 
         location.href = 'mobile.aspx'; 
    else {//keep loading the web page

2b- Check screen width

if( screen.width <= 480 ) 

    location.href = 'mobile.aspx'; 

There are 2 problems with the javascript way though.

1. Not all the devices can be detected in userAgent.
2. Not all the mobile browsers support javascript.

3- 3rd Party Databases

There are a number of third-party databases and services that can be used to determine a browser’s capabilities from a user-agent string. 51Degrees, DeviceAtlas, and WURFL are a few examples. (Detecting devices and their features – MSDN)

Hope this helps someone.

Good luck,

Leave a Reply

Your email address will not be published. Required fields are marked *