4

这是我的路线设置:

routes.MapRoute(
    name: "MyRoute",
    url: "Secret/{source}/{display}/{sort}/{tags}/{filter}/{pageSize}/{page}",
    defaults: new { controller = "Secret", action = "Index", page = 1, filter = "-", tags = "-" },
    namespaces: new[] { "SomeProject.Controllers" }
    );

当我访问此 URL 时:

http://localhost:12345/Secret/SourceHere/Gallery/Score/-/-/26

我的观点有这个标记:

<a href="@Url.Action("Index", new { tags = @tagInfo.Tag })">

那么链接中的结果 URL 是:

http://localhost:12345/Secret?tags=hello

虽然我找不到它的官方文档,但我在 SO、Google 等网站上阅读的所有内容都表示应该保留路由值。好吧,我已经通过每次指定所有路由值来解决它,但这很糟糕。

为什么我的路线值没有被保留?

编辑我下面的答案不太正确。在另一种观点/情况下,那里的规则不适用。任何人都可以绝对确定地解释何时记住路由值的规则吗?

路线让我疯狂。

4

1 回答 1

3

In turns out (thanks Robert Harvey) that making sure all route parameters have default values (of anything at all) meant the view would generate links remembering current route values.

I would like to point out that this is really confusing: the default values aren't used for anything, but they need to be specified so values are remembered!

The only thing is, it does only remember up to the first parameter whose value is being specified. Any parameters after that are lost (even those with a non-default value).

So the answer at present is: specify some (any!) default values for all route parameters, and remember to specify new values for the parameters you want, plus any coming after them in the route URL.

于 2012-11-30T23:28:00.450 回答