Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

ASP.NET Mobile Device Detection

0.00/5 (No votes)
21 Feb 2013 1  
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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here