【问题标题】:ASP.NET MVC 4 Mobile FeaturesASP.NET MVC 4 移动功能
【发布时间】:2012-03-08 08:11:34
【问题描述】:

我正在试用新的 ASP.NET MVC 4 移动功能。我用一个控制器(HomeController)和一个视图(Index)做了一个简单的应用程序。我还添加了索引视图的移动版本。

Views/Home/Index.cshtml
Views/Home/Index.Mobile.cshtml

在桌面浏览器中启动应用程序时,常规视图会按预期显示,但是当我作为三星 Galaxy S 在Opera Mobile Emulator 中启动应用程序时,我仍然看到常规视图而不是移动版本。

模拟器发送的用户代理字符串如下所示:

Opera/9.80 (Windows NT 6.1; Opera Mobi/23731; U; en) Presto/2.9.201 Version/11.50

关于为什么这不起作用的任何想法?

更新 感谢@nemesv,我能够解决这个问题,这是我目前的解决方案,希望它能涵盖大多数移动场景。

public class MobileDisplayMode : DefaultDisplayMode
{
    private readonly StringCollection _useragenStringPartialIdentifiers = new StringCollection
    {
        "Android",
        "Mobile",
        "Opera Mobi",
        "Samsung",
        "HTC",
        "Nokia",
        "Ericsson",
        "SonyEricsson",
        "iPhone"
    };

    public MobileDisplayMode() : base("Mobile")
    {
        ContextCondition = (context => IsMobile(context.GetOverriddenUserAgent()));
    }

    private bool IsMobile(string useragentString)
    {
        return _useragenStringPartialIdentifiers.Cast<string>()
                    .Any(val => useragentString.IndexOf(val, StringComparison.InvariantCultureIgnoreCase) >= 0);
    }
}

还有我 Global.asax

DisplayModeProvider.Instance.Modes.Insert(0, new MobileDisplayMode());

【问题讨论】:

  • 救命稻草。教程没有这么说,这实际上很愚蠢。从微软的角度来看,它只是感觉很糟糕。他们的教程通常很到位。
  • 谢谢,尽管 StringCollection 似乎是有史以来最没用的类之一。它不仅在此代码示例中没有提供任何内容,也没有提供性能,而且您必须编写额外的代码 (Cast) 才能使用它。将其替换为 List 并从此过上幸福的生活

标签: asp.net-mvc-4


【解决方案1】:

ASP.Net(实际上是 HttpBrowserCapabilitiesBase 类)无法将 Opera Mobile Emulator 识别为移动浏览器。

您可以在任何控制器操作中检查这一点:HttpContext.Request.Browser.IsMobileDevice 将为 Opera Mobile 浏览器返回 false

由于内置DefaultDisplayMode使用以下方法检查移动浏览器,您需要注册您的自定义DisplayMode,该DisplayMode可以正确识别Opera Mobile。

为此,您需要将其添加到 Global.asax Application_Start

DisplayModeProvider.Instance.Modes.Insert(0, new DefaultDisplayMode("Mobile")
{
    ContextCondition = (context => context.GetOverriddenUserAgent()
        .IndexOf("Opera Mobi", StringComparison.OrdinalIgnoreCase) >= 0)
});

【讨论】:

  • 谢谢!效果很好!事实证明,它也无法识别三星 Galaxy S 上的默认浏览器。我一直无法准确找出 MVC 4 框架如何验证用户代理字符串。我实现了一个类,希望能涵盖大多数场景。我已经用该代码更新了问题。
  • 您应该查看免费的 Keynote Mite 基于桌面的工具,用于测试和验证移动 Web 内容应用程序。它是一款出色的移动测试工具。 mite.keynote.com
  • 感谢您的提示,我会这样做的
  • 相当愚蠢的是 HttpBrowserCapabilities 的行为是这样的,因为 MVC 团队推荐使用 Opera 移动浏览器模拟器...asp.net/mvc/tutorials/mvc-4/aspnet-mvc-4-mobile-features 他们的 Apple Safari 用户代理字符串技巧失败了同样的原因,除非创建一个 iPhone 覆盖。
  • 完美答案!也为我做了。
【解决方案2】:

无需指定所有浏览器名称的所有手机的解决方案将是这样的......

  protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        WebApiConfig.Register(GlobalConfiguration.Configuration);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
        AuthConfig.RegisterAuth();

        DisplayModeProvider.Instance.Modes.Insert(0,
             new DefaultDisplayMode("Mobile")
             {
                 ContextCondition = (ctx => (
                     (ctx.GetOverriddenUserAgent() != null) && ctx.Request.Browser.IsMobileDevice
             ))
             });  
    }     

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-08
  • 1970-01-01
  • 2011-12-28
相关资源
最近更新 更多