如何使用ActionLink转到另一个页面asp.net MVC
您好,我在 http://localhost:1338/Tags/InternalTag/Test 上放置了一个操作链接我希望它链接到 http://localhost :1338/News/News/DisplayArticle?ArticleID=b491bee6-772c-4184-804a-13e53e50aa3d
我使用了 <%: Html.ActionLink("Test", "DisplayArticle", "News")% >
但这会导致 http://localhost:1338/Tags/News/DisplayArticle
Hi I have an action link placed on http://localhost:1338/Tags/InternalTag/Test which I would like it to link to http://localhost:1338/News/News/DisplayArticle?ArticleID=b491bee6-772c-4184-804a-13e53e50aa3d
I used <%: Html.ActionLink("Test", "DisplayArticle", "News")%>
but this results in http://localhost:1338/Tags/News/DisplayArticle
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
再次阅读后,我想我可能需要更多信息,但这是我到目前为止的建议。
我假设 InternalTags 是您的控制器,而 Test 是您的操作。
要在 URL 中包含
ArticleID
,您需要将ArticleID
添加为“routeValue
”,但这并不能完全解决问题。
您的页面位于 Tags/InternalTag/Test
您的测试页面上的链接定义为:
将解析为: http ://localhost:1338/Tags/News/DisplayArticle
如果您向链接添加 ArticleID(如下所示):
或者:
这将解析为
Tags/News/DisplayArticle?ArticleID={GUID}< /代码>。
如果我错过了一些东西,我会从不同的角度进行介绍:
对我来说,标签是您所在的网站,这将使新闻是一个完全不同的网站,如果您想去那里,您需要将链接硬编码为:http://localhost:1338/News/News/DisplayArticle?ArticleID= b491bee6-772c-4184-804a-13e53e50aa3d。
如果它们是同一站点,您将需要在 Global.asax 中整理路线。
也许您可以评论我的答案,我会相应地编辑它?
在第一条评论后编辑:
如果您能够正确获得路由映射,通常使用操作链接就足以选择正确的路由,它需要查看路由的列出方式,并更改使用的顺序和默认值,以确保签名足够具体以供选择你想要的路线。
要强制路由进行特定选择,您可以使用
Html.RouteLink(linkText,routeName,routevalues)
这将允许您控制要使用的确切路由,然后传入控制器的routeValues 、操作和文章 ID。
After reading it again I think I might need some more info, but here is what I suggest so far.
I am assuming InternalTags is your Controller, And Test is your action.
To have
ArticleID
in the URL, you need to addArticleID
as a "routeValue
"But this wont fix it completely.
Your page is located in Tags/InternalTag/Test
Your link on your test page is defined as:
Will resolve to: http://localhost:1338/Tags/News/DisplayArticle
If you add an ArticleID to the link (as below):
Alternatively:
This will resolve to
Tags/News/DisplayArticle?ArticleID={GUID}
.Incase I have missed something I will cover a different angle:
To me Tags is the site you are on, which would make News is a completely different site, if you want to go there you will need to hardcode your link as: http://localhost:1338/News/News/DisplayArticle?ArticleID=b491bee6-772c-4184-804a-13e53e50aa3d.
If they are the same site you will need to sort out the routes in your Global.asax.
Maybe you can comment on my answer and I'll edit this accordingly?
Edit After 1st comment:
If you can get the route mapping right, often using the Action link is enough to choose the correct route, it requires looking at how your routes are listed, and changing the order and default values used, to ensure the signatures are specific enough to choose the route you want.
To force the route to a specific choice you can use
Html.RouteLink(linkText,routeName,routevalues)
This will allow you to control the exact route you want to use and then pass in the routeValues for the controller, action and ArticleID.