开发者

Generating a link containing a fragment with ASP.Net MVC Routelink

I'm trying to use Html.RouteLink within a view to generate a link to a named anchor on another page. There's a few definitions for RouteLink that include a fragment option but I'm trying to figure out if there's another way.

public static string RouteLink(
    this HtmlHelper htmlHelper,
    string linkText,
    string routeName,
    string protocol,
    string hostName,
    string fragment,
    Object routeValues,
    Object htmlAttributes
)

is the obvious solution, but kind of clunky seeming. I'd prefer to be able to do something like

Html.RouteLink("Looga", new { Controller = "Cooga", Action = "Aooga", Fragment = "Fooga" })

and have that return

<a href="/Cooga/Aooga#Fooga">Looga</a>

Is that possible 开发者_运维技巧or will I need to specify every little part of the URL to get fragment using the built-in helpers. I could also just do it manually like

<a href="<%= Url.RouteUrl(new { Controller = "Cooga", Action = "Aooga" }) %>#Fooga>Looga</a>

but it seems like something RouteLink should be able to handle more elegantly.


Edited to take account of main post edits

Html.RouteLink( "Looga",
new { Controller = "Cooga", Action = "Aooga" }, new { Fragment = "Fooga" })

This code will current produce

<a href="/Cooga/Aooga" Fragment="Fooga">Looga</a>

Not really what you want. Instead, you could write your own extension method for RouteLink, something like this...

public static class RouteLinkExtensions
{
    public static string RouteLink( 
        this HtmlHelper htmlHelper, 
        string linkText, 
        object routeValues, 
        string fragment)
    {
        // There's probably better ways to do the implementation, but you get the idea
        var url = new UrlHelper(htmlHelper.ViewContext.RequestContext);
        return string.Format("<a href=\"{0}#{1}>{2}</a>",
                                url.RouteUrl(routeValues), 
                                fragment,
                                linkText); 
    }
}

This will allow you to use a clean call to RouteLink in your page :-)

Html.RouteLink( 
    "Looga",  
    new { Controller = "Cooga", Action = "Aooga" }, 
    "Fooga")


+1 to Russell.

Here's a slightly edited version of his extension which i changed to suit my purposes ... thought I would post it in case it helps someone.

Changes I made:

  • It now returns and Html string rather than a string so that you can use it for rendering the link straight into the page.

  • I changed it to accept a route name rather than a routevalues object

  • I also changed it so that if the fragment you pass in is empty it doesn't append the '#'

public static class HtmlHelperRouteLinkExtension { public static IHtmlString RouteLink( this HtmlHelper htmlHelper, string linkText, string routeName, string fragment) { var url = new UrlHelper(htmlHelper.ViewContext.RequestContext); return htmlHelper.Raw(string.Format("<a href=\"{0}{1}\">{2}</a>", url.RouteUrl(routeName), String.IsNullOrWhiteSpace(fragment) ? "" : "#" + fragment, linkText)); } }

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜