MVC 3 路由 - 单操作路由

发布于 2024-12-09 02:39:14 字数 818 浏览 0 评论 0原文

我有一个关于同一控制器中的路由/导航的问题。

假设我有以下路由设置

routes.MapLowercaseRoute("SomeAwesomeControllerLookup",
    "SomeAwesomeController/{genre}/{region}/{lookup}",
    new
    {
        controller = "SomeAwesomeController",
        action = "Index",
        genre = UrlParameter.Optional,
        region = UrlParameter.Optional,
        lookup = UrlParameter.Optional,
});

和 Url 操作,如下所示,

<a href="@Url.Action("Index", "SomeAwesomeController", new { genre = "Movies" })">Movies</a>

我希望我的 URL 每次都看起来像“/someawesomecontroller/movies”,但是当我导航到像“/someawesomecontroller/movies/boston”这样的过滤 URL 时/today”我的菜单 URL 仍然是“/someawesomecontroller/movies/boston/today”,无论上面的 HTML 片段中指定的参数如何。

我可以确保呈现的 URL 始终为“/someawesomecontroller/movies”,而无需对其进行硬编码吗?

I've got a question about routing / navigation with-in the same controller.

Lets say I've got the following route setup

routes.MapLowercaseRoute("SomeAwesomeControllerLookup",
    "SomeAwesomeController/{genre}/{region}/{lookup}",
    new
    {
        controller = "SomeAwesomeController",
        action = "Index",
        genre = UrlParameter.Optional,
        region = UrlParameter.Optional,
        lookup = UrlParameter.Optional,
});

And a Url Action as follows

<a href="@Url.Action("Index", "SomeAwesomeController", new { genre = "Movies" })">Movies</a>

I would expect my URL's to look like "/someawesomecontroller/movies" every time however when I've navigated to a filtered URL like "/someawesomecontroller/movies/boston/today" my menu URL's remain "/someawesomecontroller/movies/boston/today" regardless of the parameters specified in the HTML snipped above.

I can ensure the URL rendered is always "/someawesomecontroller/movies" without hard-coding it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

轮廓§ 2024-12-16 02:39:14

正如我其他时候了解到的,路由值被过滤并由动作渲染器使用。

<a href="@Url.Action("Index", "SomeAwesomeController", new { genre = "Movies", region="", lookup="" })">Movies</a>

应该解决你的问题。 (也可以尝试“null”,但我相信这不会覆盖现有的路由值。

As I learnt some other time, the route values are filtered down and used by the Action renderer.

<a href="@Url.Action("Index", "SomeAwesomeController", new { genre = "Movies", region="", lookup="" })">Movies</a>

Should fix your problem. (Could also try "null", but I believe that won't override the existing route values.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文