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

发布于 2024-07-11 00:09:09 字数 572 浏览 9 评论 0原文

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

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

它输出如下: More…

&hellip is ".. .”以防你想知道。

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

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

我最终得到: My-Post-Title-Here

知道如何执行此操作吗?

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…</a>

it outputs like this: More…

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

However the actionlink outputs the actual text "…" 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

浮生面具三千个 2024-07-18 00:09:09

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

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

或者,您可以“解码”传递给 ActionLink 的字符串。 在 HTML 中构建链接似乎更具可读性(对我来说)——尤其是在 Razor 中。 下面是用于比较的等价物。

@Html.ActionLink(HttpUtility.HtmlDecode("More…"), "Posts", ...)

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…</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…"), "Posts", ...)
水溶 2024-07-18 00:09:09

Sam 给出的答案实际上是正确的,我在我的解决方案中使用了它,因此我自己尝试过。
您可能需要删除多余的括号,使其变为如下所示:

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

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")
心碎的声音 2024-07-18 00:09:09

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

另一种选择:仅使用三个句点。 省略号 (U+2026) 是一个兼容性字符,仅包含在往返 Unicode 之前的编码中。 与简单的点相比,它对你的帮助微乎其微。

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.

雨轻弹 2024-07-18 00:09:09

看看这个:

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

Check out this:

  <p>Some text   @(new HtmlString(stringToPaste)) </p>
口干舌燥 2024-07-18 00:09:09

在传递值之前对其进行解码。刚刚遇到了同样的问题(不同的字符)并且工作正常:

例如:

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

虽然很烦人

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文