4

Hi I have a MVC 3 application, recently converted to mvc 4 and added mobile views. I want to add displaymodes for Mobile with desktop view for tablets specially for iPad(traffic mainly from here).

I have it like this in my code

DisplayModeProvider.Instance.Modes.Insert(0, new DefaultDisplayMode()
            {
                ContextCondition = (context => context.GetOverriddenUserAgent().IndexOf("iPad", StringComparison.OrdinalIgnoreCase) >= 0)
            });  
            DisplayModeProvider.Instance.Modes.Insert(1, new DefaultDisplayMode("Mobile")
            {
                ContextCondition = (context => context.GetOverriddenUserAgent().IndexOf("Mobile", StringComparison.OrdinalIgnoreCase) >= 0)
            });   

and Set the output cache varybycustom like below

public override string GetVaryByCustomString(HttpContext context, string custom)
  {
            string strUserAgent = context.Request.UserAgent.ToLower();
            if (strUserAgent.Contains("ipad"))
            {
                return base.GetVaryByCustomString(context, custom);
            }

            if (Request.Browser.IsMobileDevice)
            {
                    return "mobile";               
            }
            return base.GetVaryByCustomString(context, custom);
}   

I am using same urls for both mobile and desktop.

ISSUE: The Issue is after deploying the app to azure. after 1 hour the mobile gets desktop view for few urls. there is inconsistency.

can anyone help me where i am wrong. I even turned off outputcache still same issue.

tereško
  • 58,060
  • 25
  • 98
  • 150
user720719
  • 41
  • 2
  • 5

2 Answers2

5

This is a known issue, and we have recently released a workaround NuGet package for MVC 4: http://nuget.org/packages/Microsoft.AspNet.Mvc.FixedDisplayModes.

The workaround is to simply install this NuGet package to your project. If, however, your application customize the collection of the registered view engines, you should make sure that you reference to

Microsoft.Web.Mvc.FixedRazorViewEngine

or

Microsoft.Web.Mvc.FixedWebFormViewEngine

instead of the default view engine implementation.

Scott
  • 21,211
  • 8
  • 65
  • 72
Maggie Ying
  • 10,095
  • 2
  • 33
  • 36
0

This is how we are directing tablets to the desktop view:

    DisplayModeProvider.Instance.Modes.Insert(0, new DefaultDisplayMode("")
    {
        ContextCondition = (context => DeviceConfig.GetDeviceType(context.GetOverriddenUserAgent()) == "tablet")
    });

rather than creating uniqiue layouts & views for tablets, we just detect the tablets and set the DisplayModeId = "", which is the default (desktop) view.

viperguynaz
  • 12,044
  • 4
  • 30
  • 41