DisplayModeProvider中的System.NullReference

时间:2014-02-03 10:27:26

标签: mobile asp.net-mvc-5

我想在用户使用 Ipad 时显示桌面视图 我使用 DisplayModeProvide ,但有时Elmah会保留此错误:
System.Web.WebPages.DisplayModeProvider.GetDisplayInfoForVirtualPath

中的 System.NullReferenceException:

这是我的代码:

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

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:2)

我们遇到了同样的问题,请尝试检查空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)
      });