asp.net-mvc-451degrees

View Switch doesn't work with 51Degrees


I have got a MVC4 application where I used 51Degrees (Lite) to detect device and accordingly select the mobile (.mobile.cshtml) or desktop (.cshtml) view. 51Degrees can properly do that job. However if I want to switch from Mobile to Desktop view (on a mobile device) using HttpContext.SetOverriddenBrowser(BrowserOverride.Desktop) it doesn't work. FYI, it works without 51Degrees.

Here is the code to select display mode (Application_Start() in Global.asax.cs):

DisplayModeProvider.Instance.Modes.Insert(0, new DefaultDisplayMode("mobile")
 {ContextCondition = Context =>Context.Request.Browser["IsMobile"] == "True"
 });

Here is the view switcher controller action code:

public class ViewSwitcherController : Controller
{
    public RedirectResult SwitchView(bool mobile, string ReturnUrl="/Login/Login")
    {
        // If the mobile user has requested to view the mobile view
        // remove any overridden user agent for the current request
        if (Request.Browser.IsMobileDevice == mobile)
            HttpContext.ClearOverriddenBrowser();
        else
            // Otherwise override the browser setting to desktop mode
            HttpContext.SetOverriddenBrowser(mobile ? BrowserOverride.Mobile : BrowserOverride.Desktop);

        return Redirect(ReturnUrl);
    }

}

Here is the code in the view to switch to Desktop view:

@Html.ActionLink("Desktop view", "SwitchView", "ViewSwitcher", new { mobile = false, ReturnUrl = Request.Url.PathAndQuery }, new { rel = "external" })

Please let me know if I'm missing something.

Thanks in advance.


Solution

  • Sorry for my long delayed answer.

    The following solution was provided by one of the developers at 51Degrees:

    DisplayModeProvider.Instance.Modes.Insert(0, new DefaultDisplayMode("mobile")            
    {
    ContextCondition = Context => Context.GetOverriddenBrowser()["IsMobile"] == "true"
    });
    

    So replacing Context.Request.Browser["IsMobile"] with Context.GetOverriddenBrowser()["IsMobile"] fixes my problem.

    Hope that helps.