html.actionlink 的网址错误

发布于 2024-12-12 12:56:39 字数 468 浏览 0 评论 0原文

我是做了分页系统。一切都好。 单击第二页后,所有主页链接都会更改。

@Html.ActionLink("Home page", "Index", "Home") //This is standard routing without values.

我在页面末尾添加了分页链接。

@Html.ActionLink("2", "Index", "Home", New With {.id = 2}, Nothing) //This works good too.

我的问题是,当我单击第二个或更多页面(例如:www.site.com/Home/Index/2)时,我的所有主页链接都转换为

<a href="/Home/Index/2">Home page</a>

相同的。 我该如何解决这个问题?

I was made paging system. Everything is OK.
After click 2nd page all homepage links changing.

@Html.ActionLink("Home page", "Index", "Home") //This is standard routing without values.

I was added paging links to end of page.

@Html.ActionLink("2", "Index", "Home", New With {.id = 2}, Nothing) //This works good too.

My problem is when I click to 2nd or more page (e.g : www.site.com/Home/Index/2) my all homepage links converting to

<a href="/Home/Index/2">Home page</a>

same this.
How I can resolve this problem?

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

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

发布评论

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

评论(1

通知家属抬走 2024-12-19 12:56:39

当您点击第二页时,{.id = 2} 将成为您的 RouteData 的一部分。因为您的路由可能如下所示:(在 Gloabal.asax 中)

 routes.MapRoute( _
        "Default", _
        "{controller}/{action}/{id}", _
        New With {.controller = "Home", .action = "Index", .id = UrlParameter.Optional} _
    )

ASP.NET MVC 将使用此路由来生成链接。因此它将包含 Id = 2。要解决此问题,您需要在不需要时显式覆盖它:

@Html.ActionLink("Home page", "Index", "Home", New With {.id = ""}, Nothing)

When you click on the 2nd page the {.id = 2} will be part of your RouteData. And because your routing probably looks like this: (in Gloabal.asax)

 routes.MapRoute( _
        "Default", _
        "{controller}/{action}/{id}", _
        New With {.controller = "Home", .action = "Index", .id = UrlParameter.Optional} _
    )

ASP.NET MVC will use this route for the generated links. So it will include Id = 2. To fix this you need to explicitly override it when it's not needed:

@Html.ActionLink("Home page", "Index", "Home", New With {.id = ""}, Nothing)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文