我制作了我的 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/