【问题标题】:Can I make ASP.NET MVC Html.RouteLink() return a url for my home page without an ID?我可以让 ASP.NET MVC Html.RouteLink() 返回一个没有 ID 的主页的 url 吗?
【发布时间】:2010-10-14 04:58:30
【问题描述】:

我的Global.asax.vb 页面中有一条这样的路线...

Shared Sub RegisterRoutes(ByVal routes As RouteCollection) routes.IgnoreRoute("{resource}.axd/{*pathInfo}") 路线.MapRoute(_ “IdOnly”,_ “{ID}”, _ 新的 {.controller = "Page", _ .action = "详细信息", _ .id = "7"} _ ) 结束子

id 是我的主页。当有人导航到我的时显示它:http://example.com/

但是,这也有效:http://example.com/7

我不想在我的网站上的任何地方都没有指向id(共 7 个)地址的链接。但是,Html.RouteLink() 函数会生成这些链接。

对于作为主页子级的视图模型...

<%=Html.RouteLink(Model.Parent.Title, _
                  "IdOnly", _
                  New With {.id = Model.Parent.Id})%>

…生成以下锚标记:

<a href="/7">Home</a>

如何为我的主页覆盖Html.RouteLink() 函数生成的href

【问题讨论】:

    标签: asp.net-mvc routing url-routing html-helper


    【解决方案1】:

    在主页的“IdOnly”路由之前添加一个新路由:

    routes.MapRoute( _
        "HomePage", _
        "", _
        New With {.controller = "Page", _
                  .action = "Details", _
                  .id = "7"} _
    )
    

    RouteLink 将使用路由表中第一个匹配的映射来生成链接。

    【讨论】:

      猜你喜欢
      • 2017-05-21
      • 2013-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多