Domanda

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.

È stato utile?

Soluzione

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.

Altri suggerimenti

I know this is a bit dated, but I ran into this tonight. Same symptoms. Works without Mobi51, does not with. My working theory is that Request.Browser.IsMobileDevice is touched by Mobi51 and it takes control of that property and sets its value regardless of what you would expect .NET to do with it.

My current solution is this. When I check in my viewstart file to switch layouts I check that both Request.Browser.IsMobileDevice and Context.GetOverridenBrowser().IsMobileDevice are true.

When it's truly Mobile, both will be true. When it's truly desktop, both are false. When it's a mobile view requesting desktop, Request.Browser.IsMobileDevice will be true (because Mobi51 says so) and Context.GetOverridenBrowser().IsMobileDevice will be false. Here's my viewstart

@{
Layout = Request.Browser.IsMobileDevice && Context.GetOverriddenBrowser().IsMobileDevice
      ? "~/Views/Shared/_LayoutMobile.cshtml"
      : "~/Views/Shared/_Layout.cshtml";        
}

I'm still vetting this and have to add desktop to mobile switching still (which I can already see a problem, but the change to make that direction work as well is easy enough , but in my five minutes of testing so far tonight this has worked. I'm curious if you found another reason/way to work with this, or if this solution is satisfactory for you.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top