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.

5
  • What benefit could there possibly be in requiring the absolute URL as the href? Commented Sep 23, 2010 at 14:04
  • sharing the URL with jQuery or in an email? Commented Sep 23, 2010 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. Commented Sep 23, 2010 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. Commented Sep 23, 2010 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? Commented Sep 23, 2010 at 14:48

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Trying this and the @Model.Username is failing in the view. How do you access the ViewData from the view template?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.