Asp.net mvc 4 ASP.NET MVC 4移动功能

Asp.net mvc 4 ASP.NET MVC 4移动功能,asp.net-mvc-4,Asp.net Mvc 4,我正在试用新的ASP.NETMVC4移动功能。我制作了一个简单的应用程序,只有一个控制器(HomeController)和一个视图(Index)。我还添加了索引视图的移动版本 Views/Home/Index.cshtml Views/Home/Index.Mobile.cshtml 当在桌面浏览器中启动应用程序时,常规视图会按预期显示,但是当我以三星Galaxy S的身份在中启动应用程序时,我仍然会获得常规视图,而不是移动版本 从仿真器发送的用户代理字符串如下所示: Opera/9.80 (

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

Views/Home/Index.cshtml
Views/Home/Index.Mobile.cshtml
当在桌面浏览器中启动应用程序时,常规视图会按预期显示,但是当我以三星Galaxy S的身份在中启动应用程序时,我仍然会获得常规视图,而不是移动版本

从仿真器发送的用户代理字符串如下所示:

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);
    }
}
ASP.Net(实际上是
HttpBrowserCapabilitiesBase
类)不将Opera Mobile Emulator识别为移动浏览器

您可以在任何控制器操作中对此进行检查:
HttpContext.Request.Browser.IsMobileDevice
将返回Opera Mobile浏览器的
false

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

为此,您需要将其添加到Global.asax
应用程序\u Start

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

不需要指定所有浏览器名称的所有手机的解决方案如下

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

非常感谢。真是太棒了!事实证明,三星Galaxy S上的默认浏览器也无法识别。我一直无法确切了解MVC4框架如何验证用户代理字符串。我实现了一个类,希望能涵盖大多数场景。我已经用这些代码更新了这个问题。你应该看看免费的Keynote Mite桌面工具,用于测试和验证移动Web内容应用程序。它是一个优秀的移动测试工具。谢谢你的提示,我会这么做的,因为MVC团队建议使用Opera Mobile浏览器模拟器,HttpBrowserCapabilities的行为是这样的,这是相当愚蠢的。。。他们的Apple Safari用户代理字符串技巧也因同样的原因失败,除非有人创建了iPhone覆盖。完美答案!我也是。救生圈。其实很愚蠢,教程没有这样说。从微软的角度看,这感觉太差劲了。虽然StringCollection似乎是有史以来最没用的课程之一,但他们的教程通常都很贴切。谢谢。在这个代码示例中,它不仅没有提供任何功能,也没有提供任何性能,您还必须编写额外的代码(Cast)以获得使用它的乐趣。将其替换为列表并从此过上幸福快乐的生活实际上这不起作用,因为发出Request.Browser.IsMobileDevice检测移动设备的代码与选择要渲染的移动视图时执行的检查相同。
  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
             ))
             });  
    }