How do I bypass the HTML encoding when using Html.ActionLink in Mvc?

前端 未结 5 1387
醉酒成梦
醉酒成梦 2020-12-08 02:27

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



        
相关标签:
5条回答
  • 2020-12-08 02:39

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

    Eg:

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

    Annoying though

    0 讨论(0)
  • 2020-12-08 02:45

    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.

    0 讨论(0)
  • 2020-12-08 02:48

    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("&"), "Index", "Home")
    
    0 讨论(0)
  • 2020-12-08 02:55

    Check out this:

      <p>Some text   @(new HtmlString(stringToPaste)) </p>
    
    0 讨论(0)
  • 2020-12-08 03:01

    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", ...)
    
    0 讨论(0)
提交回复
热议问题