ASP.NET MVC 4 Mobile Features

后端 未结 2 392
暖寄归人
暖寄归人 2020-12-07 17:18

I\'m trying out the new ASP.NET MVC 4 Mobile Features. I made a simple app with just one controller (HomeController) and one view (Index). I also added a mobile version of t

相关标签:
2条回答
  • 2020-12-07 17:56

    a solution for all mobiles without a need to specify all browser names will be like this...

      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
                 ))
                 });  
        }     
    
    0 讨论(0)
  • 2020-12-07 17:59

    ASP.Net (actually the HttpBrowserCapabilitiesBase class) doesn't recognize the Opera Mobile Emulator as a Mobile browser.

    You can check this in any controller action: HttpContext.Request.Browser.IsMobileDevice will return false for the Opera Mobile browser.

    Because the built in DefaultDisplayMode uses the following method to check mobile browsers you need to register your custom DisplayMode which correctly recognizes Opera Mobile.

    To do this you need to add this to the Global.asax Application_Start:

    DisplayModeProvider.Instance.Modes.Insert(0, new DefaultDisplayMode("Mobile")
    {
        ContextCondition = (context => context.GetOverriddenUserAgent()
            .IndexOf("Opera Mobi", StringComparison.OrdinalIgnoreCase) >= 0)
    });
    
    0 讨论(0)
提交回复
热议问题