Mobile Device Detection in C#

WEBINAR:On-Demand

In the modern world of MVC, responsive design, and everything that surrounds the modern era of Web development, you may be forgiven for believing that you no longer have to detect the mobile devices coming to your site. You should just be able to craft your UI to "adapt" and let the browser and the device take care of the rest, right?

In an ideal world, the answer to that would be a resounding yes, but we don't live in an ideal world, so often the answer will be no. In some cases, it is still an advantage to attempt to detect the device connecting to your site and decide to give a different user experience based on those decisions.

Fortunately, ASP.NET has your back covered here. For a long time, ASP.NET has had the ability built in to determine all sorts of interesting things from the incoming connection, one of which is a simple flag telling you if the browsing client is a mobile device or not.

For this post, create yourself a new ASP.NET Web application in Visual Studio. You can use the pre-made templates if you want, but I'll be creating a blank project and just adding pages as I need them.

For me, my project template looks like this:

Figure 1: Choose an ASP.NET web application

And, in the next dialog:

Figure 2: Empty Web app with Web forms added

A little bit of a disclaimer here: I've not tested this in ASP.NET MVC but, because the request pipeline is mostly the same, the same properties should be available. If you do confirm it, please let me know in the comments.

Once you have a new project up and running, add a new Web forms page and make sure that the HTML for it looks like the following:

Detecting a Mobile Device Is Great, but We Can Do More

With the various toolkits that are about, we actually can get even more details.

For a long time now, the folks that run the "wurfl" project have been collecting ad collating data for just about every mobile device ever invented. You can find out about the project at http://wurfl.sourceforge.net/.

They provide a free to use data set that contains things like screen width, screen height, bit depth, and number of colours, as well as many other things like the types of media the device supports and lot's more.

Unfortunately, it's a little bit of a headache to dig through the data and make sense of it, so the good people at "51degrees" have made a project available both on Codeplex and as a NuGet package.

Add the package to your Web application created just a moment ago, by firing up your NuGet package manager and looking for "51Degrees.mobi", as shown in Figure 5.

Figure 5: 51 Degrees mobile detection package on NuGet

Once this has been added, you then can use the extra functionality available in your code.

Change your index.aspx page so that it looks as follows. (Remember, don't change the first line if you use different name spaces.)

You'll immediately see that we get an altogether different page. That's because, by default, 51 degrees automatically wires things up to redirect you to a different site located in the "Mobile" folder that it creates in your solution when you install it.

The test page in the mobile site allows you to see the immense range of property values the toolkit can detect. You may have also noticed that some of the properties are marked "Upgrade." This is because 51 degrees also make available a commercial toolkit that reveals much more data about the target device that, in the free version, is not available. By using the various properties and options available, you now can not only adapt your site but make be 100% aware of exactly what devices can do what, too.

.NET got you some .NOT? Drop me a comment below and maybe I can help figure out how to turn that .NOT in to a .YEA and find a .NET way of doing things.

About the Author

Peter Shaw

As an early adopter of IT back in the late 1970s to early 1980s, I started out with a humble little 1KB Sinclair ZX81 home computer.
Within a very short space of time, this small 1KB machine became a 16KB Tandy TRS-80, followed by an Acorn Electron and, eventually, after going through many other different machines, a 4MB, ARM-powered Acorn A5000.
After leaving school and getting involved with DOS-based PCs, I went on to train in many different disciplines in the computer networking and communications industries.
After returning to university in the mid-1990s and gaining a Bachelor of Science in Computing for Industry, I now run my own consulting business in the northeast of England called Digital Solutions Computer Software, Ltd. I advise clients at both a hardware and software level in many different IT disciplines, covering a wide range of domain-specific knowledge—from mobile communications and networks right through to geographic information systems and banking and finance.