2011-04-05 76 views
1

我的MVC3项目有一个叫做Mobile的区域。以下是行为与桌面浏览器和手机浏览器去我的网站时:ASP.NET MVC3路由 - 不同区域的相同URL

  1. 桌面浏览器:网址保持mydomain.com和默认桌面主页上正确显示。

  2. 移动(iPhone)浏览器:URL更改为mydomain.com/Mobile/Home,移动主页正确显示。

我希望URL保持mydomain.com,无论它是从桌面浏览器还是移动浏览器查看。我该如何完成?

回答

4

尝试使用移动设备ActionName过滤器和自定义操作方法选择。 示例(复制自'Pro ASP.NET MVC 2'书籍,页面359):

- 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"); 
      } 
     }