【发布时间】:2020-12-28 13:23:25
【问题描述】:
这几天我一直在寻找解决这个问题的方法,但它让我很难过。
我有一个 ASP.NET MVC 应用程序,我在其中实现了分页、排序和过滤。它在第一个视图中运行良好,然后当我在下一个视图中实现它时它停止工作。我正在关注this 指南。
在更改之前,如果我使用过滤器,它会发回并点击正确的操作并过滤结果。单击底部的页码之一会将我带到过滤结果集中的页面。
更改后出现错误,因为与多个匹配的路由发生冲突。由于这两个控制器的路由值相同,因此我认为我需要将控制器名称添加为路由的第一部分。
现在,当我应用过滤器时,它仍然可以工作,但是单击另一个页码会删除过滤器,但会转到正确的页码(在未过滤的结果集上)。调试告诉我,它将转到默认的 Index 操作,而不是应用过滤和排序的已定义路由。
不同之处在于,在更改之前它发送的是以下网址:
https://localhost:44382/Users/Index/UserName/ascending/none/all/2/an
现在它正在发送:
https://localhost:44382/Users/Index?sortKey=UserName&sortDirection=ascending&previousSortKey=none&selectedFbSupplied=all&page=2&selectedNameFilter=an
如果我手动将其更改为路由值而不是查询字符串,它将按预期工作。
下面代码的相关部分。
来自用户控制器:
[HttpGet]
[Route("Users/Index")]
public ActionResult Index(int? page)
{
ViewBag.SortKey = "UserName";
ViewBag.SortDirection = "ascending";
ViewBag.SelectedFbSupplied = string.IsNullOrEmpty(ViewBag.SelectedFbSupplied) ? "all" : ViewBag.SelectedFbSupplied;
ViewBag.SelectedNameFilter = string.IsNullOrEmpty(ViewBag.SelectedNameFilter) ? "" : ViewBag.SelectedNameFilter;
ViewBag.FbSupplied = new List<SelectListItem>{
new SelectListItem { Value="all", Text="All"},
new SelectListItem { Value="yes", Text="Yes"},
new SelectListItem { Value="no", Text="No"}
};
var users = SortedUserList(FilteredUsers());
int pageSize = 50;
int pageNumber = (page ?? 1);
return View(users.ToPagedList(pageNumber, pageSize));
}
[HttpGet]
[Route("Users/Index/{sortKey}/{sortDirection}/{previousSortKey}/{selectedFbSupplied}/{page:int}/{selectedNameFilter?}")]
public ActionResult Index(string sortKey, string sortDirection, string previousSortKey, string selectedFbSupplied, int? page, string selectedNameFilter="")
{
if (sortKey == previousSortKey)
{
//Key is the same, flip the direction
sortDirection = sortDirection == "ascending" ? "descending" : "ascending";
}
ViewBag.SortKey = String.IsNullOrEmpty(sortKey) ? "UserName" : sortKey;
ViewBag.SortDirection = String.IsNullOrEmpty(sortDirection) ? "ascending" : sortDirection;
ViewBag.FbSupplied = new List<SelectListItem>{
new SelectListItem { Value="all", Text="All"},
new SelectListItem { Value="yes", Text="Yes"},
new SelectListItem { Value="no", Text="No"}
};
var nameFilter = string.IsNullOrEmpty(selectedNameFilter) ? "" : selectedNameFilter;
ViewBag.SelectedFbSupplied = string.IsNullOrEmpty(selectedFbSupplied) ? "all" : selectedFbSupplied;
ViewBag.SelectedNameFilter = nameFilter;
var users = SortedUserList(FilteredUsers(nameFilter, selectedFbSupplied), sortKey, sortDirection);
int pageSize = 50;
int pageNumber = (page ?? 1);
return View(users.ToPagedList(pageNumber, pageSize));
}
以及视图中的页面链接:
@Html.PagedListPager(Model, page => Url.Action("Index", "Users" , new { sortKey = ViewBag.SortKey, sortDirection = ViewBag.SortDirection, previousSortKey = "none", selectedFbSupplied = ViewBag.SelectedFbSupplied, page = page , selectedNameFilter = ViewBag.SelectedNameFilter}))
路线配置(未更改):
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
任何想法为什么它发送查询字符串而不是路由值?
【问题讨论】:
-
必须尝试为路线指定
Name(1)(不仅仅是RouteAttribute中的Template)并使用Url.RouteUrl -
@PeterCsala 非常感谢,是的,指定名称并使用 Url.RouteUrl 已修复它。不知道为什么要这样做,我想它会因为路线相似而感到困惑。如果您发布答案,我会接受。
-
很高兴它对您有所帮助。我已经发布了一些有见地的答案。
标签: c# asp.net asp.net-mvc routes