ASP.NET MVC使用相同的控制器来分离移动视图 [英] ASP.NET MVC separate mobile views using same controllers

查看:84
本文介绍了ASP.NET MVC使用相同的控制器来分离移动视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有Razor的ASP.NET MVC 3进行一个新项目,并为同一站点开发2个不同的视图(完整版和移动版).使用相同控制器但根据用户代理显示不同视图的最佳实践/方法是什么?我应该在控制器中处理吗?在每个控制器操作中,如果if语句检查用户代理,然后根据其设备返回不同的视图,则似乎很麻烦,冗余且容易出错.

I am working on a new project using ASP.NET MVC 3 w/ Razor and developing 2 different views of the same site, a full version and a mobile version. What are the best practices/approaches for using the same controllers, but displaying different views based on the user agent? Should I handle this in the controllers? It seems that it would be cumbersome, redundant, error prone to have if statements in every controller action checking the user agent and then returning a different view depending on their device.

推荐答案

现在可以使用ASP.NET MVC(从版本4开始)的内置功能"

This can now be accomplished using a built-in feature of ASP.NET MVC (from version 4 onwards) called "DisplayModes"

默认情况下,ASP.NET MVC内置了两种显示模式.有默认的显示模式,可以像往常一样呈现您的标准"视图,还有通用的移动"显示模式.

By default, ASP.NET MVC ships with two display modes built-in. There's the default display mode, which renders your "standard" views as it always has done, and there's also a generic "mobile" display mode.

这可以通过检测客户端设备是否为移动浏览器来实现(其本身是通过嗅探客户端设备的用户代理字符串,因此并非100%可靠).如果确定该设备是移动设备,则将覆盖发送到客户端的实际MVC视图,并呈现并发送替代视图.对于附带的移动显示模式,它配置为查找后缀为.mobile.cshtml而不是.cshtml的视图(如下面的屏幕快照所示)

This works by detecting whether the client device is a mobile browser or not (which itself is determined by sniffing the User-Agent string of the client device so not 100% reliable). If the device is determined to be a mobile device, then the actual MVC view that is sent to the client is overridden and an alternative view is rendered and sent instead. In the case of the included mobile display mode, it's configured to look for a View with the suffix of .mobile.cshtml rather than .cshtml (As shown in the screenshot below)

这使您可以设计完全不同的视图,这些视图将被发送到移动设备或非移动设备,而无需对控制器逻辑进行任何更改,因此您无需添加任何条件在那里的逻辑.

This allows you to design entirely different views which will be sent to a mobile vs a non-mobile device without requiring any changes to your controller logic, so you don't need to include any conditional logic in there.

如果您需要对发送到客户端设备的确切视图进行更细粒度的控制,则整个显示模式"功能都是可配置和可扩展的.您可以定义自己的显示模式(通常在应用程序启动"中执行),这些显示模式可以特定于给定的浏览器,给定的设备或所需的任意定义.所有这些都基于来自客户端设备的用户代理字符串.

If you require more granularity of control over the exact view that's sent to the client device, the entire Display Modes functionality is configurable and extensible. You can define your own display modes (usually performed in Application Startup) that can be specific to a given browser, a given device or any arbitrary definition that you wish. All are based upon the user agent string coming from the client device.

请考虑以下代码片段,该代码片段显示了在应用程序启动"方法中为Windows Phone,iPhone和Android添加3种其他自定义显示模式:

Consider the following snippet of code below that shows adding 3 additional custom display modes for Windows Phone, iPhone and Android in the Application Start method:

protected void Application_Start()
{   
    DisplayModeProvider.Instance.Modes.Insert(0, new DefaultDisplayMode("WP")
    {
        ContextCondition = (context => context.GetOverriddenUserAgent().
            IndexOf("Windows Phone OS",StringComparison.OrdinalIgnoreCase) >= 0)
    });

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

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

为每种显示模式提供了ID和一个字符串,以与用户代理字符串相匹配,以确定是否要使用此显示模式.如果是,则 DisplayModeProvider 将查找具有相同字符串后缀的View. (即,对于上述iPhone显示模式,我们希望在用户代理字符串中的任何位置找到字符串"iPhone",如果存在,我们将使用此显示模式,该视图使用后缀iphone.cshtml而不是.cshtml呈现视图后缀.

Each display mode is given an ID and a string to match from the user agent string to determine if this display mode is to be used. If it is, the DisplayModeProvider will look for a View with the same string suffix. (i.e. For the iPhone display mode above, we expect to find the string "iPhone" anywhere in the user agent string, and if it exists, we use this display mode which renders views with the iphone.cshtml suffix rather than a .cshtml suffix.

您可以在此处阅读有关此功能的更多信息: http://www.asp.net/mvc/overview/older-versions/aspnet-mvc-4-mobile-features 特别是在覆盖视图,布局和部分视图"和特定于浏览器的视图"部分中.

You can read more about this functionality here: http://www.asp.net/mvc/overview/older-versions/aspnet-mvc-4-mobile-features specifically in the sections headed "Overriding Views, Layouts, and Partial Views" and "Browser-Specific Views".

这篇关于ASP.NET MVC使用相同的控制器来分离移动视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆