gpt4 book ai didi

c# - ASP.NET MVC 路由 - Url.Action 发送查询字符串而不是路由值

转载 作者:行者123 更新时间:2023-12-05 06:11:47 24 4
gpt4 key购买 nike

几天来我一直在寻找这个问题的解决方案,但它让我难住了。

我有一个 ASP.NET MVC 应用程序,我在其中实现了分页、排序和过滤。它在第一个 View 中运行良好,然后当我在下一个 View 中实现它时它停止工作了。我在关注 this指导。

在更改之前,如果我使用过滤器,它会发回并点击正确的操作并过滤结果。单击底部的页码之一会将我带到过滤结果集中的页面。

更改后我收到一个错误,因为与多个匹配的路由发生冲突。由于这两个 Controller 的路由值相同,我认为我需要将 Controller 名称添加为路由的第一部分。

现在,当我应用过滤器时,它仍然有效,但随后单击另一个页码会删除过滤器,但会转到正确的页码(在未过滤的结果集上)。调试显示它将转到默认索引操作,而不是将应用过滤和排序的定义路由。

不同之处在于,在更改之前它发送以下 url:

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

如果我手动将其更改为路由值而不是查询字符串,它将按预期工作。

下面代码的相关部分。

来自用户 Controller :

    [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));
}

以及 View 中的页面链接:

@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 }
);
}

}

知道为什么它发送的是查询字符串而不是路由值吗?

最佳答案

简而言之:如果您指定路由名称(不仅仅是路由模板),那么您可以通过 RouteUrl 通过它们的名称来引用它们 helper 。

路线模板

  • 它是一个模式,它定义了给定的 Action 如何到达以及路由参数应该如何匹配。
  • 它们被注册到 RouteTable 中,RouteTable 基本上是给定 url 模式与关联 Controller 操作之间的映射。
  • 路线的顺序很重要,因为第一个匹配者获胜。

路线名称

  • 它们与 Url 模式匹配无关。
  • 它们仅在 Url 生成期间使用。 (RouteUrlCreatedAtRoute(1)等)
  • 它们必须是全局唯一的(因此顺序无关紧要)。

可能的错误

  • 如果您使用相同的 Template 注册了两个路由,那么当您对其中一个路由进行调用时,应用程序将在运行时抛出一个 AmbiguousMatchException
    • 因此,其他路由也能正常工作。
  • 如果您使用相同的 Name 注册了两个路由,那么当您进行 any 调用时,应用程序将在运行时抛出一个 InvalidOperationException
    • 因此,其他路线将行不通。

关于c# - ASP.NET MVC 路由 - Url.Action 发送查询字符串而不是路由值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63823019/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com