繁体   English   中英

单击routelink URL不会将所有数据传递给操作

[英]Clicking on routelink url not passing all the data to action

我已经使用RouteLink创建URL。

@Html.RouteLink("Edit", "PageWithId",
new
{
    controller = "Customers",
    action = "Edit",
    id = item.CustomerID,
    page = ViewBag.CurrentPage
}) 

我正在将此路由PageWithId与路由链接一起使用

routes.MapRoute(
    name: "PageWithId",
    url: "{controller}/{action}/{page}/{id}",
    defaults: new { controller = "Customers", action = "Edit", page = UrlParameter.Optional, id = UrlParameter.Optional }
);

我有3个路由代码。 这就是全部

routes.MapRoute(
    name: "PageWithSort",
    url: "{controller}/{action}/{page}/{SortColumn}/{CurrentSort}",
    defaults: new { action = "Index", page = UrlParameter.Optional, SortColumn = UrlParameter.Optional, CurrentSort = UrlParameter.Optional }
);

routes.MapRoute(
    name: "PageWithId",
    url: "{controller}/{action}/{page}/{id}",
    defaults: new { controller = "Customers", action = "Edit", page = UrlParameter.Optional, id = UrlParameter.Optional }
);

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

当我运行程序时,路由链接会生成类似http://localhost:55831/Customers/Edit/1/ALFKI URL

当我单击链接时,将调用“编辑”操作,但客户ID却为null ,因为URL中有ALFKI作为客户ID。

这是我的编辑操作详细信息

public ActionResult Edit(string id, int page)
{
    if (id == null)
    {
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
    }
    Customer customer = db.Customers.Find(id);
    if (customer == null)
    {
        return HttpNotFound();
    }
    ViewBag.CurrentPage = page;
    return View(customer);
}

请告诉我,当ALFKI作为客户ID传递时,为什么id会为null?

占位符(例如{page}{controller} )的作用类似于变量。 它可以匹配任何内容并将其存储在具有相同名称的路由值中。

因此,您实际上对配置说的是:

尝试

/<anything (optional)>/<anything (optional)>/<anything (optional)>/<anything (optional)>/<anything (optional)>

如果这不起作用,请尝试

/<anything (optional)>/<anything (optional)>/<anything (optional)>/<anything (optional)>

如果这不起作用,请尝试

/<anything (optional)>/<anything (optional)>/<anything (optional)>

如果这不起作用,则由于{id}是可选的,请尝试

/<anything (optional)>/<anything (optional)>

如果这样不起作用,由于{action}是可选的,请尝试

/<anything (optional)> (if it matches, action will be "Index")

如果这不起作用,则由于{controller}是可选的,请尝试

/ (if it matches, controller will be "Home" and action will be "Index")

看到问题了吗? 第一条路由可以(并且将)匹配任何值且长度在0到5段之间的URL。 一旦匹配,就无法尝试遵循它的另一条路线。

您需要以某种方式限制路由,以确保某些URL可以传递到下一条尝试的路由,即在您不想匹配该URL的情况下。

同样,可以将段设为可选 ,但不能更改position 如果传递的值表示CurrentSort ,则自动表示它左侧的所有参数都是必需的

在我看来,将{page}{SortColumn}{CurrentSort}设为可选是不合逻辑的。 如果不需要它们,则可以使用其他路由,也可以将它们完全从路径中移出,而使用查询字符串(路由不受查询字符串值的影响,它们仅在路径上匹配)。

要使值成为必需, value = UrlParameter.Optional从默认value = UrlParameter.Optional删除value = UrlParameter.Optional 实际上,您可以删除所有默认值,因为控制器和操作已在URL中传递。

routes.MapRoute(
    name: "PageWithSort",
    url: "{controller}/{action}/{page}/{SortColumn}/{CurrentSort}"
);

routes.MapRoute(
    name: "PageWithId",
    url: "{controller}/{action}/{page}/{id}"
);

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

尽管这些路由仍然可以匹配任何内容 ,但这会将它们限制为特定的长度,并且由于所有这三个路由都具有不同数量的细分,因此它们不会冲突。 但是,由于它们仍然如此广泛,因此您应该考虑使用另一种技术来约束它们,例如文字段或路线约束。 例如:

routes.MapRoute(
    name: "CustomerEditWithId",
    url: "Customers/Edit/{page}/{id}",
    defaults: new { controller = "Customers", action = "Edit" }
);

应用第一个路由PageWithSort而不是第二个路由。

请注意,路由框架不知道您用于生成URL的参数名称。 它只是查看收到的字符串Customers/Edit/1/ALFKI

对于此字符串,第一个路由“ PageWithSort”与

  • 控制器=客户
  • 动作=编辑
  • 页= 1
  • SortColumn = ALFKI
  • CurrentSort = null(这是可以的,因为它是可选的)

因为将使用第一个匹配的路由,所以该URL将永远不会命中“ PageWithId”路由。

也许可以使“ PageWithSort”的SortColumnCurrentSort参数成为必需?

或更改定义路由的顺序。

或添加“ PageWithSort”路由特有的内容,例如

routes.MapRoute(
        name: "PageWithId",
        url: "{controller}/{action}/{page}/ID/{id}",
        //                                 ^^
        defaults: new { controller = "Customers", action = "Edit", page = UrlParameter.Optional, id = UrlParameter.Optional }
    );

/ID添加为URL的硬编码部分将使其与其他路由区分开(假设没有SortColumn调用“ ID”)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM