2011-04-05 108 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"); 
      } 
     }