【发布时间】:2012-04-10 02:15:18
【问题描述】:
我有一个 MVC3 站点,其中包含 2 个区域和公共区域。我还指定了用于对项目列表进行分页的路线。我的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
在这一点上,我相当肯定我在路线图上搞砸了,但对我来说似乎是对的。我做错了什么?
更新 根据建议,我将路线更改为如下所示:
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 并选择了最接近路径的答案。
【问题讨论】:
标签: c# asp.net-mvc asp.net-mvc-3 asp.net-mvc-routing