0

我的应用程序中设置了两条路线:

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 的约束,但我不想这样做。还有其他人对如何解决这个问题有任何其他建议吗?

4

2 回答 2

1

这应该有效。我怀疑您Model.Id的值大于 9,因此无法通过仅允许一位数的约束。所以尝试通过允许多个数字来调整约束:

routes.MapRoute(
    "CustomerActivity",
    "Customer/{id}",
    new { controller = "CustomerActivity", action = "DisplayDetails" },
    new { id = @"^\d+$" }
);
于 2012-09-18T16:23:04.977 回答
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"
                            });
于 2012-09-18T18:16:40.910 回答