gpt4 book ai didi

asp.net-mvc - 如何让 Url.Action 不保留路由值?

转载 作者:行者123 更新时间:2023-12-01 08:41:05 25 4
gpt4 key购买 nike

我有一个 MVC 2.0 应用程序,其中有一个可以按首字母浏览的项目列表,并且在母版页上有一个选项卡,用于显示整个项目列表。

我想要的是母版页上的选项卡始终应该转到:

/items

并且有 abc 的字母,这些字母转到以字母开头的项目。因此,例如对于 a 它应该转到:
/items/a

要为母版页中的选项卡生成链接,我使用:
Url.Action("Index", "Items")

问题是,如果我查看以 a 开头的项目,所以我在 /items/a ,那么选项卡也指向这个位置,而不仅仅是 /items

任何人都知道这是为什么,我该怎么做才能使用正确的 UrlHelper 方法始终指向选项卡中的 /items

编辑:我的路线如下:
    routes.MapRouteLowercase(
"DefaultPrefixed", // Route name
"{controller}/{action}/{q}", // URL with parameters
new { controller = "Items", action = "Index", q = "" }, // Parameter defaults
new { q = @"[a-z]{1}" } // constraint
);

routes.MapRouteLowercase(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);

最佳答案

这将取决于您的路线,但假设您使用的是默认路线:

routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Users", action = "Index", id = UrlParameter.Optional }
);

您始终可以将可选的路由值设置为空白:
Url.Action("Index", "Items", new { id = string.Empty })

关于asp.net-mvc - 如何让 Url.Action 不保留路由值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3130807/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com