asp.net-mvc - 将 iPad 设备的 MVC 设置为使用常规 Web 版本,而不是 .mobile

标签 asp.net-mvc asp.net-mvc-4

我制作了我的 mvc 网站的移动版本(.mobile View ),它可以在移动设备上正常工作,但在模拟 ipad 时,它也使用 .mobile 版本。

我如何告诉mvc它应该为ipad设计和其他非移动设备选择网页的常规版本?

最佳答案

我们通过在全局 Application_Start 方法中添加以下内容来修复此问题。

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

这告诉 MVC 使用默认 View 而不是 iPad 设备的移动 View 。

实际上,我们在配置文件中列出了“排除的设备”列表。

// 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)
    );
}

此外,在我们的应用程序中,我们需要通过单个配置设置禁用所有移动 View (无需删除所有移动 View 文件)。这使我们能够根据需要打开和关闭移动 View 。再次在 Application_Start 中,我们找到并删除 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);
    }
}

关于asp.net-mvc - 将 iPad 设备的 MVC 设置为使用常规 Web 版本,而不是 .mobile,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22621603/

相关文章:

c# - 路径 '/' 的 Controller 未找到或未实现 IController

javascript - jsonresult 数据集传递给另一个actionresult

javascript - ASP.net MVC - View 和 jQuery 最佳实践

android - 使用 HTML5 启用后置摄像头

asp.net-mvc-3 - MVC 3/4 中 URL 的语言 SEO 优化

jquery - 在 ASP.NET MVC 中呈现 jQuery jqGrid 插件后,我可以更改其双击事件处理程序 (ondblClickRow) 吗?

asp.net-mvc - MVC 逻辑应该放在 Controller 还是 View 模型中

azure - MVC 4 Azure Web角色: Error 500 when adding the "tracing" element to web.配置

javascript - Jquery new Date 返回错误结果

asp.net-mvc-4 - asp.net webapi 发布 - xml 文件不复制