My latest work involved creating the mobile version of some pages of a web application.
Once the pages conversion was relatively completed in the "~/Mobile/" path, I had to create the logic to redirect users to the mobile version of the "mobiled" pages automatically. This logic had to also permit users to navigate the desktop pages from their mobile devices with a "Desktop version" button.
The first task was the mobile device detection. I could have, of course, find "ios" and "android" in the user-agent, but that would not have been the "programmers way" and many devices (windows, blackberry, etc) would have circumvented detection, and we don't want that!
Some Google time pointed to the excellent open source MobileESP, whose ASP.Net implementations are already very nifty. Unfortunately I was not able to use the library as is for two reasons: The application was written in VB.Net and neither of the provided examples (base page and user control) could really be used in this environment.
What was a programmer supposed to do? Well, a previous programmer had created a common utility C# project, so the MobileESP code could be placed there. So, I extracted all the functional detection code and supporting data of "Sub-Classed Page Technique", placed it there, and I was halfway on my detection heaven! Here is the detection class I wrote.
From that point on, it was all a matter of implementing the logic to redirect to the correct pages when necessary in the existent base page; which I am pasting underneath
'''
''' Determines if it is necesary to redirect to the mobile
''' version of the current page and redirects if appropiate.
''' The check is done by checking if the current user has overriden
''' the setting by clicking the "desktop version".
'''
'''
Public Sub RedirectToMobileVersion()
If Not Session("RedirectOverride") Is Nothing And Session("RedirectOverride") = "1" Then
Exit Sub 'The user selected the desktop version
End If
Dim mobileDetector As MobileDeviceDetection = New MobileDeviceDetection(request)
If mobileDetector.DetectMobileLong() Or mobileDetector.DetectTierTablet() Then
Session("RedirectOverride") = Nothing 'Reset the redirect
Dim url As String = String.Format("~/mobile{0}", Request.RawUrl.ToLower())
Response.Redirect(url)
End If
End Sub
'''
''' Redirects to the desktop version of the current page, setting the page override before doing so.
'''
'''
Public Sub RedirectToDesktopVersion()
Session("RedirectOverride") = "1" 'Set the redirect override
Dim url As String = String.Format("~{0}", Request.RawUrl.ToLower().Replace("/mobile/", "/"))
Response.Redirect(url)
End Sub
The method RedirectToMobileVersion, which does what it's name indicates, is called on the page pre_init in all the pages that have a mobile version. As you can see this method's first action is to check the Session("RedirectOverride") variable. This variable is set when the user navigates to the desktop version of the site on a mobile device and prevents the automatic redirection to leave him scratching his head with a big "huh?" face. You might have noticed that the detection uses both the "DetectMobileLong", which the documentation mentions should detect most devices, including iPads, and the "DetectTierTablet" method. Why? Well, in our testing we noticed that without calling out the tablet test specifically, iPad 2's were not being detected.
The method RedirectToDesktopVersion but before doing so, it sets the Session("RedirectOverride") variable (whose existence and function was explained above).
As you might have noticed, I am using the Request.RawUrl. Why you might ask? Well, that way the full URL, with query string parameters and all is used, and since the parameters are used the same way on the mobile and non mobile versions, this ensures smooth, flawless navigation between versions!
Hoping this is of any use to you,
Stay programming my friends!
Rod
MobileDeviceDetection.cs (40.34 kb)