MikeAlike234
MikeAlike234

Reputation: 759

Set MVC for IPad devises to use regular web version, not .mobile

I have made a mobile version of my mvc site (.mobile views) and it's works ok with mobile, but when emulating ipad it's also using .mobile version.

How can i tell mvc that it should pick regular version of the web-page for ipad devised and other non mobile devises?

Upvotes: 2

Views: 851

Answers (2)

robmzd
robmzd

Reputation: 1823

We fixed this by adding the following in the global Application_Start method.

DisplayModeProvider.Instance.Modes.Insert(0, new DefaultDisplayMode()
{
    ContextCondition = (context => context.Request.UserAgent != null && context.GetOverriddenUserAgent().IndexOf("iPad", StringComparison.OrdinalIgnoreCase) >= 0)
});

This tells MVC to use the default views rather than mobile views for iPad devices.

Actually we have a list of 'excluded devices' that are listed in the configuration file.

// For each excluded device, set to use the default (desktop) view
foreach (var excludedMobileDevice in ExcludedDevices)
{
    DisplayModeProvider.Instance.Modes.Insert(0, new DefaultDisplayMode()
    {
        ContextCondition = (context => context.Request.UserAgent != null && context.GetOverriddenUserAgent().IndexOf(excludedMobileDevice, StringComparison.OrdinalIgnoreCase) >= 0)
    );
}

Also in our application, we have a requirement to disable all of the mobile views through a single configuration setting (without having to remove all of the mobile view files). This enables us to turn the mobile views on and off as required. Again in Application_Start we find and remove the display mode with the Id "Mobile".

// Remove the built in MVC mobile view detection if required
if (!MobileViewEnabled)
{
    var mobileDisplayModeProvider = DisplayModeProvider.Instance.Modes.FirstOrDefault(d => d.DisplayModeId == "Mobile");

    if (mobileDisplayModeProvider != null)
    {
        DisplayModeProvider.Instance.Modes.Remove(mobileDisplayModeProvider);
    }
}

Upvotes: 4

logikal
logikal

Reputation: 1141

This sounds like a known bug with MVC4

There is a fix - upgrade to MVC5 or install the following NuGet package: Install-Package Microsoft.AspNet.Mvc.FixedDisplayModes

Further reading about the NuGet Package. http://www.nuget.org/packages/Microsoft.AspNet.Mvc.FixedDisplayModes

Further reading about the bug: http://forums.asp.net/t/1840341.aspx, http://aspnetwebstack.codeplex.com/workitem/280

Upvotes: 0

Related Questions