2012-04-10 69 views
2

我有一個MVC3網站有2個區域加公共區域。我也有一個路由指定分頁項目列表。我Register_Routes方法看起來像這樣:MVC3與區域的路線...有點混亂

public static void RegisterRoutes(RouteCollection routes) 
    { 
     routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 

     routes.MapRoute(
      "Paginate", // Route name 
      "{controller}/Paginate/{itemsPerPage}/{pageNumber}/{searchString}", // URL with parameters 
      new { controller = "Home", action = "Index", itemsPerPage = SiteSettings.ItemsPerPage, pageNumber = 1, searchString = UrlParameter.Optional } // Parameter defaults 
     ); 

     routes.MapRoute(
      "Default", // Route name 
      "{controller}/{action}/{id}", // URL with parameters 
      new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults 
     ); 

    } 

我注意到什麼(不知道)是,如果我從我的主頁登出,從我的登錄頁面重定向看起來像

http://localhost:62695/Account/LogOn?ReturnUrl=%2fHome%2fPaginate 

...並在登錄,我結束了在我的主頁,除了與的網址:

http://localhost:62695/Home/Paginate 

我在這一點上,我搞砸事情了路線圖相當肯定,但對我來說似乎是正確的。我究竟做錯了什麼?

UPDATE 每建議,我改變了我的路線是這樣的:

public static void RegisterRoutes(RouteCollection routes) 
    { 
     routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 

     routes.MapRoute(
      "Paginate", // Route name 
      "{controller}/Paginate/{itemsPerPage}/{pageNumber}/{searchString}", // URL with parameters 
      new { controller = "Home", action = "Index", searchString = UrlParameter.Optional } // Parameter defaults 
     ); 

     routes.MapRoute(
      "Default", // Route name 
      "{controller}/{action}/{id}", // URL with parameters 
      new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults 
     ); 

    } 

...和家庭索引頁確實似乎能正常工作,但現在做進行分頁不是:

 return RedirectToAction("Paginate", new { itemsPerPage = SiteSettings.ItemsPerPage, pageNumber = 1, searchString = string.Empty }); 
在管理\ HomeController的

產生的網址:

http://localhost:62695/Admin/Users/Paginate?itemsPerPage=25&pageNumber=1 

所以我仍然在這裏做錯了什麼。

更新2 OK,這就是我得到它的工作我想它的方式: 我RegisterRoutes方法現在看起來是這樣的:

public static void RegisterRoutes(RouteCollection routes) 
    { 
     routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 

     routes.MapRoute(
      null, 
      "{area}/{controller}/Paginate/{itemsPerPage}/{pageNumber}/{searchString}", // URL with parameters 
      new {area = string.Empty, controller = "Home", action="Paginate", searchString = UrlParameter.Optional } // Parameter defaults 
     ); 

     routes.MapRoute(
      "Default", // Route name 
      "{area}/{controller}/{action}/{id}", // URL with parameters 
      new {area = string.Empty, controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults 
     ); 
    } 

...但是這是不夠的解決路由問題。除此之外,我需要將路線添加到我的區域註冊。我AdminAreaRegistration看起來是這樣的:

public override void RegisterArea(AreaRegistrationContext context) 
    { 
     context.MapRoute(
      null, 
      "Admin/{controller}/Paginate/{itemsPerPage}/{pageNumber}/{searchString}", // URL with parameters 
      new { controller = "Home", action = "Paginate", searchString = UrlParameter.Optional } // Parameter defaults 
     ); 

     context.MapRoute(
      "Admin_default", 
      "Admin/{controller}/{action}/{id}", 
      new { action = "Index", id = UrlParameter.Optional } 
     ); 
    } 

此,除了改變以RedirectToRoute我聯繫,讓我的網址都相當,並在同一時間工作。所有答案都有助於實現我的目標,我爲每個人+1,並選擇了讓我最接近路徑的答案。

回答

1
routes.MapRoute(null, // do not name your routes, it's a "magic string" 
    "{controller}/Paginate/{itemsPerPage}/{pageNumber}/{searchString}", 
     new 
     { 
      controller = "Home", 
      action = "Index", 
      searchString = UrlParameter.Optional 
     } 
    ); 

// instead of RedirectToAction, try RedirectToRoute, and do not use the route name 
return RedirectToRoute(new 
    { 
     controller = "Home", 
     area = "AreaName", 
     itemsPerPage = SiteSettings.ItemsPerPage, 
     pageNumber = 1, 
     searchString = string.Empty, 
    } 
); 
+0

這似乎沒有什麼區別。 – 2012-04-10 12:40:39

+0

我忘記了地區路線片段,並更新了我的答案。 Paginate URL應該映射到什麼操作方法? 'HomeController.Index(int itemsPerPage,int pageNumber,string searchString)?'如果不是,則需要更改'MapRoute'。 – danludwig 2012-04-10 16:11:28

1

那麼,您返回到網址http://localhost:62695/Home/Paginate的原因是因爲當您登錄時,您將返回到指定的URL,即部分http://localhost:62695/Account/LogOn?ReturnUrl=%2fHome%2fPaginate。這不是你的主頁的URL嗎?你從未指定過。

它可能是第一個定義也優先,也不知道我聽到的地方,所以也許如果你先把默認定義,它會抓住。

+0

嗯,看到了,cecilphillip也是這樣想。嘗試重新排序您的路線定義。 – 2012-04-10 02:26:20

+0

嗯......我的主頁應該是'/',簡單明瞭。所以不要定義默認值......嗯。 – 2012-04-10 02:34:23

2

路由按它們註冊的順序進行評估。重定向是通過您註冊的第一條路線生成的,尤其是在您爲所有分段聲明瞭默認值之後。您可能要考慮定義更明確的路徑

UPDATE 使用RedirectToRoute而不是RedirectToAction來獲得您想要的URL生成

RedirectToRoute("Paginate", new { itemsPerPage = SiteSettings.ItemsPerPage, pageNumber = 1, searchString = string.Empty }); 
+0

好吧,如果我刪除'itemsPerPage'和'pageNumber'的默認值,我的主頁似乎工作正常......但我的RedirectToAction值現在列出東西在標準的GET格式(即,http:// localhost :62695/Admin/Users/Paginate?itemsPerPage = 25&pageNumber = 1'),而不是我想要的漂亮的'http:// localhost:62695/Admin/Users/Paginate/25/1'。 – 2012-04-10 02:40:16

+0

您應該改用RedirectToRoute。我已經更新了上面的答案 – cecilphillip 2012-04-10 15:33:07