我的应用程序中设置了两条路线:
routeCollection.MapRoute("CustomerActivity",
"Customer/{id}",
new { controller = "CustomerActivity", action = "DisplayDetails" },
new { id = @"^\d$" });
routeCollection.MapRoute("CustomerSearch",
"Customer/{*pathInfo}",
new
{
controller = "CustomerSearch",
action = "DisplaySearch"
});
传入的 URL 被正确地路由到正确的控制器/动作配对。但是在视图中,我需要生成一个 Anchor 来查看客户的详细信息:
@Html.ActionLink(Model.Name, "DisplayDetails", "CustomerActivity", new { id = Model.Id }, null)
问题在于它实际上并没有选择任何路线。我相信这是因为对 CustomerActivity 路线的限制。
我也试过使用RouteLink
没有太多运气:
@Html.RouteLink(Model.Name, "CustomerActivity", new { id = Model.Id })
我无法取消对 CustomerActivity 的约束,因为这是阻止一切落入该路线的原因。
在没有约束的情况下添加 CustomerActivity 的副本似乎可以解决问题,但我没有留下深刻的印象:
routeCollection.MapRoute("CustomerActivity",
"Customer/{id}",
new { controller = "CustomerActivity", action = "DisplayDetails" },
new { id = @"^\d$" });
routeCollection.MapRoute("CustomerSearch",
"Customer/{*pathInfo}",
new
{
controller = "CustomerSearch",
action = "DisplaySearch"
});
routeCollection.MapRoute("CustomerActivityUrlCreation",
"Customer/{id}",
new { controller = "CustomerActivity", action = "DisplayDetails" });
我能想到的唯一另一件事是从根本上区分 URL 并摆脱对 CustomerActivity 的约束,但我不想这样做。还有其他人对如何解决这个问题有任何其他建议吗?