1

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:-

  1. 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.

  2. 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.

cw_dev
  • 465
  • 1
  • 12
  • 27
  • How do you set the DisplayModeProvider? May be the string from the User-Agent that you are comparing is outdated and doesn't include support for Galaxy Browser. The string for Galaxy should look like: http://stackoverflow.com/questions/11282944/what-is-the-samsung-s3-user-agent – Fals Sep 19 '13 at 12:49
  • I set is as follows... DisplayModeProvider.Instance.Modes.Insert(0, New DefaultDisplayMode("Mobile") With { _ .ContextCondition = (Function(ctx) ctx.Request.UserAgent.IndexOf("iPad", StringComparison.OrdinalIgnoreCase) < 0 AndAlso (ctx.GetOverriddenBrowser().IsMobileDevice OrElse (ctx.GetOverriddenUserAgent().IndexOf("Android", StringComparison.OrdinalIgnoreCase) >= 0 AndAlso ctx.Request.UserAgent.IndexOf("mobile", StringComparison.OrdinalIgnoreCase) >= 0))) _ }). However, I think my problem is with the SwitchView action rather that the initial DisplayModeProvider. – cw_dev Sep 19 '13 at 13:27
  • ... the page initially loads the Mobile view correctly. It's just I can't switch between Desktop and Mobile correctly on an Android Galaxy S3 when the server is a Windows 2003. – cw_dev Sep 19 '13 at 13:28

0 Answers0