我有一個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,並選擇了讓我最接近路徑的答案。
這似乎沒有什麼區別。 – 2012-04-10 12:40:39
我忘記了地區路線片段,並更新了我的答案。 Paginate URL應該映射到什麼操作方法? 'HomeController.Index(int itemsPerPage,int pageNumber,string searchString)?'如果不是,則需要更改'MapRoute'。 – danludwig 2012-04-10 16:11:28