[英]MVC3 Routes with areas... a little confusion
我有一個包含 2 個區域和公共區域的 MVC3 站點。 我還指定了一條用於對項目列表進行分頁的路線。 我的Register_Routes
方法如下所示:
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
......登錄后,我最終進入了我的主頁,但 URL 除外:
http://localhost:62695/Home/Paginate
在這一點上,我相當確定我在路由 map 上搞砸了,但這對我來說似乎是對的。 我究竟做錯了什么?
根據建議更新,我將路線更改為如下所示:
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 });
在 Admin\HomeController 中產生 URL:
http://localhost:62695/Admin/Users/Paginate?itemsPerPage=25&pageNumber=1
所以我仍然在這里做錯事。
更新 2好的,這就是我讓它按照我想要的方式工作的方式:我的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
之外,還使我的 URL 變得漂亮並且同時工作。 所有的答案都有助於實現我的目標,我為每個人 +1,並選擇了讓我最接近路徑的答案。
路由按照它們注冊的順序進行評估。 重定向是由您注冊的第一條路線生成的,特別是因為您為所有段聲明了默認值。 您可能需要考慮定義更具體的路線
更新使用 RedirectToRoute 而不是 RedirectToAction 來獲得所需的 URL 代
RedirectToRoute("Paginate", new { itemsPerPage = SiteSettings.ItemsPerPage, pageNumber = 1, searchString = string.Empty });
那么,您返回到 URL http://localhost:62695/Home/Paginate
的原因是因為當您登錄時,您返回到指定的 URL,即 http://localhost 的?ReturnUrl=%2fHome%2fPaginate
部分http://localhost:62695/Account/LogOn?ReturnUrl=%2fHome%2fPaginate
。 那不是你主頁的URL嗎? 你從來沒有指定。
可能第一個定義也優先,不知道我在哪里聽說過,所以如果你把默認定義放在第一位,它可能會抓住它。
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,
}
);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.