IsMobileDevice returns False for Android (On some servers)
I am developing a website using MVC 4 that has both Desktop and Mobile
views. I have created a DisplayModeProvider entry in my Global.asax that
allows iPhone's or Android mobiles.
I would like the user to be able to switch between Mobile and Desktop at
the click of a button. So I created a view that changes between Desktop
and Mobile as follows...
@If (ViewContext.HttpContext.GetOverriddenBrowser().IsMobileDevice) Then
@Html.ActionLink("Desktop View", "SwitchView", "Navigation", New With
{.Mobile = False, .ReturnUrl = Request.Url.PathAndQuery}, New With
{.rel = "external", .data_role = "button", .data_inline = "true"})
Else
@<a href="@Url.Action("SwitchView", "Navigation", New With {.Mobile =
True, .ReturnUrl = Request.Url.PathAndQuery})">Mobile View</a>
End If
... and this is the action...
Public Function SwitchView(Mobile As Boolean, ReturnUrl As String) As
RedirectResult
If Request.Browser.IsMobileDevice = Mobile Then
HttpContext.ClearOverriddenBrowser()
Else
HttpContext.SetOverriddenBrowser(If(Mobile,
BrowserOverride.Mobile, BrowserOverride.Desktop))
End If
Return Redirect(ReturnUrl)
End Function
This works perfectly on iPhone's.
But now I am testing with an Samsung Galaxy S3 (Android). On a Windows 7
IIS this works perfectly. However, when I run the same website on a 2003
Server IIS it does not work.
I think this is because the browser files
C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\Config\Browsers are
outdated, therefore IsMobileDevice returns False for Androids.
Questions:-
Is it possible to modify the code above to make Android's work? I am not
sure what I would change in the Action to allow this to work.
Alternatively, is it possible at project level to modify the browser files
so that Android's would return True for IsMobileDevice on a 2003 Server? I
don't want to update the browser definition files on the machine itself
because we distribute our website to dozens of different customers and a
project level solution would be preferred.
No comments:
Post a Comment