在Mvc中使用Html.ActionLink时如何绕过HTML编码?
每当我使用 Html.ActionLink 时,它总是对我的显示字符串进行 Html 编码。 例如,我希望我的链接看起来像这样:
<a href="/posts/422/My-Post-Title-Here">More…</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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
看起来 ActionLink 总是在链接文本上使用 HttpUtility.Encode 调用。 您可以使用 UrlHelper 生成 href 并自行构建锚标记。
或者,您可以“解码”传递给 ActionLink 的字符串。 在 HTML 中构建链接似乎更具可读性(对我来说)——尤其是在 Razor 中。 下面是用于比较的等价物。
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.
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.
Sam 给出的答案实际上是正确的,我在我的解决方案中使用了它,因此我自己尝试过。
您可能需要删除多余的括号,使其变为如下所示:
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:
或者,只需使用纯 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.
看看这个:
Check out this:
在传递值之前对其进行解码。刚刚遇到了同样的问题(不同的字符)并且工作正常:
例如:
虽然很烦人
Decode it before passing the value in. Just had this same issue (different characters) and it works fine:
Eg:
Annoying though