2012-03-08 100 views
32

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

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

当启动在桌面浏览器应用程序的常规示意图如图预期,但是当我启动在Opera Mobile Emulator作为三星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); 
    } 
} 

而且我的Global.asax

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

拯救生命。它真的很愚蠢,教程不会说这个。它只是从微软方面感觉如此半屁股。他们通常与他们的教程保持一致。 – 2012-09-20 14:29:40

+1

谢谢,虽然StringCollection似乎是有史以来最无用的类之一。它不仅没有提供这个代码示例中的任何内容,也不提供性能,因此您不得不编写额外的代码(Cast ),以便使用它。用替换它,然后快乐地生活 – PandaWood 2014-09-30 07:03:51

回答

28

ASP.Net(实际上是HttpBrowserCapabilitiesBase类)不能识别的Opera Mobile模拟器的手机浏览器。

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

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

要做到这一点,你需要它添加到Global.asax中Application_Start

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

谢谢!这很好用!事实证明,它没有识别出三星Galaxy S上的默认浏览器。我一直无法找到MVC 4框架如何验证用户代理字符串。我实现了一个希望能覆盖大多数场景的类。我已经用该代码更新了问题。 – Pelle 2012-03-09 21:35:30

+5

您应该查看免费的基于Keynote Mite桌面的工具来测试和验证移动Web内容应用程序。这是一款出色的移动测试工具。 http://mite.keynote.com/ – 2012-03-12 13:06:44

+0

感谢您的提示,我会做到这一点 – Pelle 2012-03-14 20:57:02

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

实际上这不会工作,根据http://stackoverflow.com/questions/12710026/how-does-mvc4-detect-a-mobile-browser使得Request.Browser.IsMobileDevice检测移动设备的相同代码是与选择移动视图进行渲染时所执行的检查相同。 – JonVD 2014-01-09 06:13:53