我怎么会改变根据设备类型ASP.NET MVC的看法? [英] How Would I Change ASP.NET MVC Views Based on Device Type?

查看:91
本文介绍了我怎么会改变根据设备类型ASP.NET MVC的看法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过一些ASP.NET MVC阅读我的路,我在工作中有一个web应用程序,我会从迁移到的WebForms MVC。一个我期望在过程中得到的功能要求的是,如果用户从移动设备来已经返回的简化图。

I'm working my way through some ASP.NET MVC reading and I have a web app at work that I'll be migrating from WebForms to MVC. One of the feature requests I expect to get in the process is to have a simplified view returned if the user is coming from a mobile device.

我不能完全看到的最好的地方是实施类型的逻辑。我敢肯定有比在每一个返回一个视图操作添加的if / else为Browser.IsMobileDevice一个更好的办法。我想有什么样的选择,这样做呢?

I can't quite see where the best place is to implement that type of logic. I'm sure there's a better way than adding an if/else for Browser.IsMobileDevice in every action that returns a view. What kind of options would I have to do this?

推荐答案

更新:该解决方案有一个微妙的错误。与 useCache将= TRUE FindView / FindPartialView 两次code>,如果不与 useCache将= FALSE返回结果,一旦。既然是所有类型的视图只有一个缓存,移动用户可能会最终看到桌面的意见,如果桌面浏览器是第一个到达的。

Update: This solution has a subtle bug. The MVC framework will call into FindView/FindPartialView twice: once with useCache=true, and if that doesn't return a result, once with useCache=false. Since there's only one cache for all types of views, mobile users may end up seeing desktop views if a desktop browser was first to arrive.

对于那些有兴趣使用自定义视图引擎来解决这个问题,斯科特Hanselman有更新,在这里他的解决办法:

For those interested in using custom view engines to solve this problem, Scott Hanselman has updated his solution here:

<一个href=\"http://www.hanselman.com/blog/ABetterASPNETMVCMobileDeviceCapabilitiesViewEngine.aspx\">http://www.hanselman.com/blog/ABetterASPNETMVCMobileDeviceCapabilitiesViewEngine.aspx

(道歉的答案劫持,我只是不希望其他人必须要经过这个!)

(Apologies for the answer hijack, I just don't want anyone else to have to go through this!)

按roufamatic编辑(2010-11-17)

你想要做的第一件事是推出移动设备浏览器文件到项目中。使用此文件可以针对您希望在不必知道什么这些设备在他们的头发送的具体支持什么都设备。这个文件已经为你做的工作。然后,使用Request.Browser属性进行量身定制一个视图要返回。

The first thing you want to do is introduce the Mobile Device Browser File to your project. Using this file you can target what ever device you want to support without having to know the specifics of what those devices send in their headers. This file has already done the work for you. You then use the Request.Browser property to tailor which view you want to return.

接下来,拿出你想如何组织视图文件夹下你的观点的战略。我preFER离开桌面版在根,然后有一个移动的文件夹。比如在主页视图文件夹是这样的:

Next, come up with a strategy on how you want to organize your views under the Views folder. I prefer to leave the desktop version at the root and then have a Mobile folder. For instance the Home view folder would look like this:


  • 首页结果

    • 移动搜索

      • iPhone结果

        • 的Index.aspx


        • 的Index.aspx

        我有@Mehrdad不同意有关使用自定义视图引擎。该视图引擎提供不止一个目的,这些目的之一是寻找意见为控制器更多。您可以通过重写FindView方法做到这一点。在这种方法中,你可以做你在哪里可以找到该视图检查。你知道哪些设备正在使用你的网站后,你可以使用你想出了组织你的意见返回该设备的视图中的策略。

        I have to disagree with @Mehrdad about using a custom view engine. The view engine serves more than one purpose and one of those purposes is finding views for the controller. You do this by overriding the FindView method. In this method, you can do your checks on where to find the view. After you know which device is using your site, you can use the strategy you came up with for organizing your views to return the view for that device.

        public class CustomViewEngine : WebFormViewEngine
        {
            public override ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache)
            {
                // Logic for finding views in your project using your strategy for organizing your views under the Views folder.
                ViewEngineResult result = null;
                var request = controllerContext.HttpContext.Request;
        
                // iPhone Detection
                if (request.UserAgent.IndexOf("iPhone",
           StringComparison.OrdinalIgnoreCase) > 0)
                {
                    result = base.FindView(controllerContext, "Mobile/iPhone/" + viewName, masterName, useCache);
                }
        
                // Blackberry Detection
                if (request.UserAgent.IndexOf("BlackBerry",
           StringComparison.OrdinalIgnoreCase) > 0)
                {
                    result = base.FindView(controllerContext, "Mobile/BlackBerry/" + viewName, masterName, useCache);
                }
        
                // Default Mobile
                if (request.Browser.IsMobileDevice)
                {
                    result = base.FindView(controllerContext, "Mobile/" + viewName, masterName, useCache);
                }
        
                // Desktop
                if (result == null || result.View == null)
                {
                    result = base.FindView(controllerContext, viewName, masterName, useCache);
                }
        
                return result;
            }
        }
        

        以上code可让您设置根据你的战略的看法。回退是桌面视图,如果没有视图被发现的设备或如果没有一个默认的移动视图。

        The above code allows you set the view based on your strategy. The fall back is the desktop view, if no view was found for the device or if there isn't a default mobile view.

        如果你决定把逻辑控制器的而不是创建一个视图引擎。最好的方法是创建一个自定义的<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.mvc.actionfilterattribute.aspx\">ActionFilterAttribute你可以装饰你的控制器。然后重写<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.mvc.actionfilterattribute.onactionexecuted.aspx\">OnActionExecuted方法来确定哪个设备正在浏览您的网站。您可以检查此博客帖子就如何。该职位也有一些不错的链接到一些混合的视频在这个主题。

        If you decide to put the logic in your controller's instead of creating a view engine. The best approach would be to create a custom ActionFilterAttribute that you can decorate your controller's with. Then override the OnActionExecuted method to determine which device is viewing your site. You can check this blog post out on how to. The post also has some nice links to some Mix videos on this very subject.

        这篇关于我怎么会改变根据设备类型ASP.NET MVC的看法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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