asp.net-mvc 在 Mvc 中使用 Html.ActionLink 时如何绕过 HTML 编码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/422929/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-07 23:15:58  来源:igfitidea点击:

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

asp.net-mvchtml-encode

提问by Micah

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

每当我使用 Html.ActionLink 时,它总是对我的显示字符串进行 Html 编码。例如,我希望我的链接看起来像这样:

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

it outputs like this: More&hellip;

它的输出是这样的:更多…

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

&hellip 是“...”,以防你想知道。

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

然而,actionlink 输出实际的文本“…” 作为链接文本。如果我想输出这个,我有同样的问题:

<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>

我结束了: <em>My-Post-Title-Here</em>

Any idea how to do this?

知道如何做到这一点吗?

回答by tvanfosson

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.

看起来 ActionLink 总是在链接文本上使用 HttpUtility.Encode 调用。您可以使用 UrlHelper 生成 href 并自己构建锚标记。

<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.

或者,您可以“解码”传递给 ActionLink 的字符串。在 HTML 中构建链接似乎更具可读性(对我而言) - 特别是在 Razor 中。下面是比较的等效项。

@Html.ActionLink(HttpUtility.HtmlDecode("More&hellip;"), "Posts", ...)

回答by oogway

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:

Sam给出的答案实际上是正确的,我在我的解决方案中使用了它,因此我自己尝试过。您可能想要删除额外的括号,使其变成这样:

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

回答by Pavel Shkleinik

Check out this:

看看这个:

  <p>Some text   @(new HtmlString(stringToPaste)) </p>

回答by bobince

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.

或者,只需使用普通的 Unicode 省略号 \u2026 并让 MVC 担心如何对其进行编码。除非有一些特别令人信服的理由,否则您特别需要一个 helip 实体引用而不是字符引用,或者只是将字符包含为简单的 UTF-8 字节。

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.

另一种选择:只需使用三个句点。省略号 (U+2026) 是一个兼容字符,仅包含在往返 Unicode 之前的编码中。与简单的点相比,它给你带来的影响很小。

回答by Sam

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

虽然很烦