Click here to Skip to main content
Click here to Skip to main content

Tagged as

ASP.NET Mobile Device Detection

, 21 Feb 2013
Rate this:
Please Sign up or sign in to vote.
Detect latest mobile devices/browsers using ASP.NET and 51degrees

Introduction

In ASP.NET, you can easily detect the mobile device request using Request.Browser.IsMobileDevice property and Request.UserAgent.

The following code checks the IsMobileDevice property and redirects to the mobile specific page:

protected void Page_Load(object sender, EventArgs e)
{
    if (Request.Browser.IsMobileDevice)
    {
       Response.Redirec("~/default_mobile.aspx");          
    }
} 

If you request "default.aspx" from mobile browser, it will redirect to default_mobile.aspx page

Step 1: Request from Mobile User Agent

Change the User Agent (Safari Browser).

AspnetMobDeviceDetection/aspnet-mobile-detection-step2.JPG

Step 2: Request "default.aspx"

A "default.aspx" request automatically redirects to "default_mobile.aspx" because HTTP request is from mobile user agent.

AspnetMobDeviceDetection/aspnet-mobile-detection-step3.JPG

Problem with Latest Browsers

Some of the popular mobile devices/browsers won’t be detected using this way because ASP.NET browser files are not supported in Opera Mobile or Android devices. That means if you request "default.aspx" from Opera mobile browser, it won't redirect to "default_mobile.aspx" Frown | :-(

AspnetMobDeviceDetection/aspnet-mobile-detection-step4.JPG

Solution

You can fix this problem using 51Degrees.Mobi package. 51Degrees.Mobi package is an open source .NET project that enhances Request.Browser using from Wireless Universal Resource File (WURFL). WURFL is one of the comprehensive and up-to-date databases of mobile device information.

You can easily install/download the 51Degrees using NuGet package manager in VS 2010.

Install 51Degrees

Step 1: Open NuGet Package Manager

AspnetMobDeviceDetection/aspnet-mobile-detection-step5.JPG

Step 2: Install 51Degrees

AspnetMobDeviceDetection/aspnet-mobile-detection-step6.JPG

"Install-Package 51Degrees.mobi" command automatically adds "FiftyOne.Foundation.dll" reference and necessary files under "App_Data" folder as shown below:

AspnetMobDeviceDetection/aspnet-mobile-detection-step7.JPG

AspnetMobDeviceDetection/aspnet-mobile-detection-step8.JPG

Step 3: Define 51Degrees Configuration Section

AspnetMobDeviceDetection/aspnet-mobile-detection-step9.JPG

Step 4: Define 51Degrees/wurfl

AspnetMobDeviceDetection/aspnet-mobile-detection-step10.JPG

The above configuration changes automatically update the ASP.NET browser detection capabilities and you can easily detect the modern mobile devices/browsers. If you request "default.aspx" from Opera mobile browser after this configuration changes, it will automatically redirect to "default_mobile.aspx" page as shown below. Smile | :)

AspnetMobDeviceDetection/aspnet-mobile-detection-step12.JPG

Conclusion

I hopes you got some idea about 51Degrees, WURFL and ASP.NET browser detection. Thanks for reading. This is the same as my original post ASP.NET mobile device detection.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

A Mahesh
Web Developer
India India
Symphony Services, Banglore, 560 087, India
Follow on   Twitter

Comments and Discussions

 
GeneralMy vote of 5 PinmemberRene Pilon22-Feb-13 15:17 
GeneralMy vote of 5 Pinmemberffernandez2322-Feb-13 1:07 
Question51 Degrees Value Add PinmemberRTS@Orix5-Dec-12 4:30 
QuestionMissing Files in App_Data PinmemberPraveen Rajendran21-Nov-12 4:09 
QuestionWURFL Use Pinmemberazaz289-Sep-12 21:01 
Hi,
 
Very simple and nice article. My question is how you are using WURFL XML file. Have you paid WURFL against that XML file or what?
 
Thanks
 
azaz
GeneralMy vote of 5 PinmemberSk. Tajbir11-Jun-12 21:03 
QuestionNuget package install creates dependency on Microsoft.Web.Infrastructure Pinmemberjaimepremy8-Nov-11 8:29 
GeneralRe: Nuget package install creates dependency on Microsoft.Web.Infrastructure PinmemberMember 918720728-Jun-12 21:48 
Question51Degrees PinmemberTomholmes5-Aug-11 4:51 
QuestionA good article Pinmemberrippo21-Jun-11 20:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140827.1 | Last Updated 21 Feb 2013
Article Copyright 2011 by A Mahesh
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid