gpt4 book ai didi

c# - ActionLink 显示错误的网址

转载 作者:太空宇宙 更新时间:2023-11-03 12:59:47 25 4
gpt4 key购买 nike

我在路由表中有一些预定义的 URL 实际上指向相同的 Controller 和操作,但语言不同。例如

            routes.MapRoute(
"Contact", // Route name
"contact", // URL with parameters
new { controller = "Home", action = "Contact", lang="en" } // Parameter defaults
);

routes.MapRoute(
"Contact2", // Route name
"iletisim", // URL with parameters
new { controller = "Home", action = "Contact", lang="tr" } // Parameter defaults
);

当我在查看和运行应用程序中使用 Html.ActionLink 时,URL 显示第一个路由,例如:

@Html.ActionLink(Resources.Contact, "Contact", "Home")

当当前语言是 tr 而页面呈现的 URL 是:/contact 应该是 /iletisim

当我更改路由映射的顺序时,en 语言页面显示错误的 URL。

我该如何解决这个问题?

最佳答案

匹配来自 ActionLink 的路由时,您必须考虑该路由生成的所有参数。该框架将始终返回所提供路由值的第一个匹配项。

在您的例子中,您提供了路由值:

controller = "Home"
action = "Contact"

考虑到缺少其他信息,这将匹配您的 Contact 路线(第一条路线 controller = "Home", action = "Contact")。

如果要匹配特定的路由,那么还需要传递lang路由值。

@Html.ActionLink(Resources.Contact, "Contact", "Home", new { lang = "tr" }, null)

这将匹配 Contact2 路由。

或者,您可以将 lang 放入页面的 URL 中,这将确保它是当前请求的一部分,如 ASP.NET MVC 5 culture in route and url 所示。 .然后 MVC 会自动将此值提供给您所有的 ActionLinks 和其他基于 UrlHelper 的方法。

关于c# - ActionLink 显示错误的网址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32653165/

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