Click here to Skip to main content
15,867,568 members
Articles / Web Development / ASP.NET

ASP.NET Mobile Device Detection

Rate me:
Please Sign up or sign in to vote.
4.85/5 (24 votes)
21 Feb 2013CPOL2 min read 176.7K   67   12
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:

C#
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)


Written By
Web Developer
India India
Symphony Services, Banglore, 560 087, India

Comments and Discussions

 
QuestionNuget package install creates dependency on Microsoft.Web.Infrastructure Pin
Jaime Premy8-Nov-11 8:29
professionalJaime Premy8-Nov-11 8:29 
GeneralRe: Nuget package install creates dependency on Microsoft.Web.Infrastructure Pin
Member 918720728-Jun-12 21:48
Member 918720728-Jun-12 21:48 

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

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