【问题标题】:ASP.NET MVC Routing - Url.Action sending query string not route valuesASP.NET MVC 路由 - Url.Action 发送查询字符串而不是路由值
【发布时间】: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


【解决方案1】:

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

路线模板

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

路线名称

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

可能的错误

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-17
    • 1970-01-01
    • 1970-01-01
    • 2011-11-15
    • 2017-06-22
    • 1970-01-01
    • 2010-11-01
    相关资源
    最近更新 更多