Gal Ratner
Gal Ratner is a Techie who lives and works in Los Angeles CA and Austin TX. Follow galratner on Twitter Google
How to detect a mobile device in ASP.NET

If you have a mobile version of your site, you probably need to detect and redirect users on mobile devices that navigate into the main site. It’s easy in ASP.NET you can just call

Request.Browser.IsMobileDevice

Or

Request.Browser["IsMobileDevice"]

Or even

System.Web.HttpBrowserCapabilities myBrowserCaps = Request.Browser;
((System.Web.Configuration.HttpCapabilitiesBase)myBrowserCaps).IsMobileDevice;

You also need to add <httpRuntime useFullyQualifiedRedirectUrl = "true" /> if you’re doing any redirects with mobile since This sends a fully qualified URL to the client with  asession ID.
But what happens under the hood and why doesn’t ASP.NET detect my new phone? Well it’s simple. ASP.NET uses the request’s User-Agent and matches it to one of the Browser Definition Files. The default files are in C:\Windows\Microsoft.NET\Framework\v2.0.50727\CONFIG\Browsers and since they don’t get updated often, there is a good chance your new phone is not recognized. Luckily you can create your own browser files by simply creating a folder named App_Browsers and composing your own files. It’s great, but, not very practical. Unless you keep track of every new phone that comes out to the market and have the time to keep updating your files, you will quickly find yourself wishing there was some sort of ready file you could use.

Codeplex to the rescue

Mobile Device Browser File  is a project on codeplex that gets regular updates and contains most of the new devices on the market. You can download it here: http://mdbf.codeplex.com/  installation is super simple. Just Copy the mobile.browser file into the  "App_Browsers\Devices" folder. If you don’t have any, create one.

Update (8/29/2011): Mobile Device Browser File is no longer supported. 51degrees.mobi is the recommended solution now. You can download it from codeplex here.

That’s it. Your site can now  detect most of the new mobile devices on the market.


Posted 4 Mar 2010 7:54 AM by Gal Ratner
Filed under: ,

Powered by Community Server (Non-Commercial Edition), by Telligent Systems