42

Whenever I use Html.ActionLink it always Html encodes my display string. For instance I want my link to look like this:

<a href="/posts/422/My-Post-Title-Here">More&hellip;</a>

it outputs like this: More&hellip;

&hellip is "..." incase you were wondering.

However the actionlink outputs the actual text "&hellip;" as the link text. I have the same problem with if I want to output this:

<a href="/posts/422/My-Post-Title-Here"><em>My-Post-Title-Here</em></a>

I wind up with: <em>My-Post-Title-Here</em>

Any idea how to do this?

Eddie
  • 53,828
  • 22
  • 125
  • 145
Micah
  • 111,873
  • 86
  • 233
  • 325

5 Answers5

81

It looks like ActionLink always uses calls HttpUtility.Encode on the link text. You could use UrlHelper to generate the href and build the anchor tag yourself.

<a href='@Url.Action("Posts", ...)'>More&hellip;</a>

Alternatively you can "decode" the string you pass to ActionLink. Constructing the link in HTML seems to be slightly more readable (to me) - especially in Razor. Below is the equivalent for comparison.

@Html.ActionLink(HttpUtility.HtmlDecode("More&hellip;"), "Posts", ...)
John B
  • 20,062
  • 35
  • 120
  • 170
tvanfosson
  • 524,688
  • 99
  • 697
  • 795
  • @tvanfosson almost 4 years later... is this still the best method for stuffing special characters like ampersands, carets, etc. into link text? – one.beat.consumer Nov 08 '12 at 20:44
  • @one.beat.consumer I think so, but there is an alternative which I've added. Also updated to reflect Razor syntax. – tvanfosson Nov 08 '12 at 23:52
  • Mvc Html extensions are all rubbish, for more power try Fubu MVC's HtmlTags library, you can then write your own (better) extension methods that return HtmlTags rather than MvcHtmlString – Sam Dec 10 '13 at 22:29
  • For MVC5 they renamed the Decode method to HtmlDecode @Html.ActionLink(HttpUtility.HtmlDecode("More…"), "Posts", ...) – klingu Feb 18 '14 at 11:27
10

The answer given by Sam is actually correct and I used it in my solution so I have therefore tried it myself. You may want to remove the extra parenthesis so it becomes something like this:

@Html.ActionLink(HttpUtility.HtmlDecode("&amp;"), "Index", "Home")
oogway
  • 111
  • 1
  • 5
7

Alternatively, just use a plain Unicode ellipsis character \u2026 and let MVC worry about how to encode it. Unless there's some particularly compelling reason you'd specifically need a hellip entity reference as opposed to a character reference or just including the character as simple UTF-8 bytes.

Alternative alternatively: just use three periods. The ellipsis (U+2026) is a compatibility character, only included to round-trip to pre-Unicode encodings. It gets you very little compared to simple dots.

bobince
  • 528,062
  • 107
  • 651
  • 834
  • 1
    The ellipsis is more semantically meaningful than three periods. It's also usually rendered more nicely. – bdesham Oct 15 '12 at 16:29
6

Check out this:

  <p>Some text   @(new HtmlString(stringToPaste)) </p>
Pavel Shkleinik
  • 6,298
  • 2
  • 24
  • 36
4

Decode it before passing the value in. Just had this same issue (different characters) and it works fine:

Eg:

@Html.ActionLink(HttpUtility.HtmlDecode(_("&amp;")), "Index", "Home")

Annoying though

Sam
  • 1,725
  • 1
  • 17
  • 28