MVC 4移动和平板电脑视图分离

jas*_*koh 6 c# asp.net-mvc

我正在尝试为平板电脑和移动设备制作单独的视图.在app_start我有这个代码

DisplayModeProvider.Instance.Modes.Insert(0, new
        DefaultDisplayMode("Tablet")
        {
            ContextCondition = (ctx =>
            ctx.Request.UserAgent.IndexOf("iPad", StringComparison.OrdinalIgnoreCase) >= 0 ||
            ctx.Request.UserAgent.IndexOf("Android", StringComparison.OrdinalIgnoreCase) >= 0)
        });
Run Code Online (Sandbox Code Playgroud)

我创建了两个布局文件,一个用于移动设备,另一个用于平板电脑.但是当我从Android上的移动设备访问时存在冲突.它将我重定向到layout.tablet.我怎么能分开这两个设备?

jas*_*koh 8

我在浏览器中使用用户代理切换器对此进行了测试,它运行正常.

DisplayModeProvider.Instance.Modes.Insert(0, new
        DefaultDisplayMode("Tablet")
        {
            ContextCondition = (ctx =>
            ctx.Request.UserAgent.IndexOf("iPad", StringComparison.OrdinalIgnoreCase) >= 0 ||
            ctx.Request.UserAgent.IndexOf("Android", StringComparison.OrdinalIgnoreCase) >= 0 && 
            ctx.Request.UserAgent.IndexOf("mobile", StringComparison.OrdinalIgnoreCase) < 1)
        });

        DisplayModeProvider.Instance.Modes.Insert(1, new DefaultDisplayMode("Mobile")
        {
            ContextCondition = (ctx =>
                ctx.GetOverriddenBrowser().IsMobileDevice)
        });
Run Code Online (Sandbox Code Playgroud)