我可以让ASP.NET MVC Html.RouteLink()返回我的主页的URL没有ID?
-
20-08-2019 - |
题
我有一个路由在我Global.asax.vb
页是这样的...
Shared Sub RegisterRoutes(ByVal routes As RouteCollection) routes.IgnoreRoute("{resource}.axd/{*pathInfo}") routes.MapRoute( _ "IdOnly", _ "{id}", _ New With {.controller = "Page", _ .action = "Details", _ .id = "7"} _ ) End Sub
这id
是我的主页。当有人浏览到我的它显示:http://example.com/
但是,这也适用:http://example.com/7
我想有没有联系我的网站上的任何地方用的7 id
但是,在Html.RouteLink()
函数生成的。
有关这主页的子视图模型......
<%=Html.RouteLink(Model.Parent.Title, _
"IdOnly", _
New With {.id = Model.Parent.Id})%>
...生成以下锚点标记:
<a href="/7">Home</a>
我如何可以覆盖href
函数生成的Html.RouteLink()
我的主页?
解决方案
在主页的“IdOnly”路线前添加一个新的路线:
routes.MapRoute( _
"HomePage", _
"", _
New With {.controller = "Page", _
.action = "Details", _
.id = "7"} _
)
RouteLink
将使用第一匹配映射在路由表中,以生成的链接。
不隶属于 StackOverflow