ASP.NET MVC3 Routing - Same URL for different areas

时间秒杀一切 提交于 2019-12-07 18:41:29

Try to use ActionName filter and custom action method selector for mobile device. Example (copy from 'Pro ASP.NET MVC 2' book, page 351):

- In Controller define 2 function for desktop & iPhone, they have the same ActionName

    [iPhone]
    [ActionName("Index")] 
    public ActionResult Index_iPhone() { /* Logic for iPhones goes here */ }     
    [ActionName("Index")]
    public ActionResult Index_PC() { /* Logic for other devices goes here */ }

- Define [iPhone] action method selector:           
    public class iPhoneAttribute : ActionMethodSelectorAttribute 
        { 
            public override bool IsValidForRequest(ControllerContext controllerContext,  
                                                   MethodInfo methodInfo) 
            { 
                var userAgent = controllerContext.HttpContext.Request.UserAgent; 
                return userAgent != null && userAgent.Contains("iPhone"); 
            } 
        }
John Sobolewski
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!