2009-12-07 94 views
0

有沒有辦法將RouteLink作爲鏈接的名稱?RouteLink作爲鏈接名稱

<%= Html.RouteLink(...., "myRoute", new { id = 75 }) %> 

被渲染爲

<a href="http://foo/Something/75">http://foo/Something/75</a> 

有一些巧妙的技巧是什麼?

/M

回答

2

您可以創建一個擴展方法來爲您處理

public static string PrintRouteLink (this HtmlHelper helper, string routeName, int id) 
{ 
    UrlHelper url = new UrlHelper(helper.ViewContext.RequestContext); 
    return helper.RouteLink(url.RouteUrl(routeName,new { id = id}),routeName,new { id = id}); 
} 

然後你可以使用:

<%= Html.PrintRouteLink(routeName,75) %>