ASP.NET MVC ActionLink with LinkText = absolute URL
Asked Answered
C

1

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.

Cameo answered 23/9, 2010 at 14:1 Comment(5)
What benefit could there possibly be in requiring the absolute URL as the href?Urger
sharing the URL with jQuery or in an email?Nymphomania
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.Urger
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.Cameo
OK, I see what you're saying about the href not necessarily requiring being the absolute URL, but what about the LinkText?Cameo
M
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.

Marita answered 24/9, 2010 at 20:20 Comment(1)
Trying this and the @Model.Username is failing in the view. How do you access the ViewData from the view template?Inflow

© 2022 - 2024 — McMap. All rights reserved.