5
<%: Html.ActionLink("Share Me", "Index", "Mall", new { username = Model.Username }, null)%>

results as expected according to mapped routes:

<a href="/Mall/Username">Share Me</a>

I however need it to not say Share Me but to show the absolute URL, as well as use the absolute URL as the href:

<a href="http://www.url.com/Mall/Username">http://www.url.com/Mall/Username</a>

I feel this isn't a hard task, but being very green in tackling MVC I'm having a hard time figuring it out.

Nick Spiers
  • 2,344
  • 2
  • 19
  • 31
  • What benefit could there possibly be in requiring the absolute URL as the href? – Kirk Woll Sep 23 '10 at 14:04
  • sharing the URL with jQuery or in an email? – Stefanvds Sep 23 '10 at 14:10
  • For the latter, my understanding is that right-clicking on a URL (in any browser) and choosing "Copy Link Location" will *always* return the absolute URL irrespective of whether a relative URL was used. For the former, why would it be helpful even in jQuery to have absolute URLs? Relative URLs should work fine in jQuery as much as it does for normal links. – Kirk Woll Sep 23 '10 at 14:15
  • It's a URL tailored to the user for sharing, and it prompts them to copy it. A novice user could easily be fooled into copying the link text and trying to share it. You know how in emails it shows the absolute URL for you to paste into the navigation bar if the link doesn't work? Like that. – Nick Spiers Sep 23 '10 at 14:39
  • OK, I see what you're saying about the href not necessarily requiring being the absolute URL, but what about the LinkText? – Nick Spiers Sep 23 '10 at 14:48

1 Answers1

9

Rather than using Html.ActionLink, you should have a look at Url.RouteUrl (which ActionLink uses internally anyway). Something like...

<% var myUrl = Url.RouteUrl("Default", new { action = "Mall", username = Model.Username }, Request.Url.Scheme).ToString() %>

<a href="<%:myUrl%>"><%:myUrl%></a>

Note the first parameter is the route name.

Clicktricity
  • 4,171
  • 1
  • 23
  • 15