我在我的应用程序中设置了两条路线:
routeCollection.MapRoute("CustomerActivity",
"Customer/{id}",
new { controller = "CustomerActivity", action = "DisplayDetails" },
new { id = @"^\d$" });
routeCollection.MapRoute("CustomerSearch",
"Customer/{*pathInfo}",
new
{
controller = "CustomerSearch",
action = "DisplaySearch"
});
传入的网址已正确路由到正确的控制器/操作配对。但是在视图中我需要生成一个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" });
我能想到的另一件事就是从根本上区分Urls并摆脱对CustomerActivity的约束,但我不想这样做。有没有其他人就如何解决这个问题提出任何其他建议?
答案 0 :(得分:1)
这应该有效。我怀疑你的Model.Id
大于9,因此你的约束失败,只允许一个数字。因此,请尝试通过允许多个数字来调整约束:
routes.MapRoute(
"CustomerActivity",
"Customer/{id}",
new { controller = "CustomerActivity", action = "DisplayDetails" },
new { id = @"^\d+$" }
);
答案 1 :(得分:0)
Try this...It will work
routeCollection.MapRoute("CustomerActivity",
"{controller}/{action}/{id}",
new { controller = "CustomerActivity", action = "DisplayDetails" },
new { id = @"^\d$" });
routeCollection.MapRoute("CustomerSearch",
"CustomerSearch/DisplaySearch/{*pathInfo}",
new
{
controller = "CustomerSearch",
action = "DisplaySearch"
});