Mobile DisplayModeProvider中的System.NullReference

Mobile DisplayModeProvider中的System.NullReference,mobile,asp.net-mvc-5,Mobile,Asp.net Mvc 5,我想在用户使用Ipad时显示桌面视图。 我使用显示模式提供,但有时Elmah会保留此错误: System.NullReferenceException:位于System.Web.WebPages.DisplayModeProvider.GetDisplayInfoForVirtualPath中 这是我的代码: if (DisplayModeProvider.Instance != null) if (DisplayModeProvider.Instance.Modes != nul

我想在用户使用Ipad时显示桌面视图。
我使用显示模式提供,但有时Elmah会保留此错误:
System.NullReferenceException:位于System.Web.WebPages.DisplayModeProvider.GetDisplayInfoForVirtualPath中

这是我的代码:

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

有人能帮我吗?

我们遇到了同样的问题,请尝试检查空的
UserAgent

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

谢谢@robmzd,我用:
//Ipad var userAgent=Request.userAgent;如果(!string.IsNullOrEmpty(userAgent)&&(userAgent.IndexOf(“iPad”)>-1))HttpContext.setoverridedenbrowser(browserverride.Desktop)