gpt4 book ai didi

asp.net-mvc - Url.Action 产生查询字符串,有什么方法可以产生完整的 url?

转载 作者:行者123 更新时间:2023-12-04 20:42:10 26 4
gpt4 key购买 nike

目前在我的项目中,我正在使用 @Url.Action 在 anchor 标签的 href 属性中创建链接。

例如:

<a href='@Url.Action("Index", "Home", new { id = 10 })' id="btn">Click</a>

这很好用,但是生成的 URL 是......
/home/index?id=10

有没有办法返回网址
/home/index/10

用于 SEO 和美学目的?我使用 id 和 number 作为占位符 - 在实际应用程序中,它可以并且将使用字符串来代替(所以...
<a href='@Url.Action("Index", "Home", new { name="test" })' id="btn">Click</a>

返回网址
/home/index/test

最佳答案

您需要确保路由正确。确保您有这样的路线:

routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional });

对于您的“实际应用程序”,因为您正在使用一个名为 name 的参数你需要一个看起来像这样的:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{name}",
defaults: new { controller = "Home", action = "Index" });

关于asp.net-mvc - Url.Action 产生查询字符串,有什么方法可以产生完整的 url?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24260997/

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