如何在MVC中使用Html.ActionLink当我绕过HTML编码? [英] How do I bypass the HTML encoding when using Html.ActionLink in Mvc?

查看:135
本文介绍了如何在MVC中使用Html.ActionLink当我绕过HTML编码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我用Html.ActionLink它总是连接的Html codeS我的显示字符串。比如我想我的链接看起来像这样:

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>

其输出是这样的:更多&放大器; hellip;

it outputs like this: More&hellip;

&放大器; hellip是...柜面你想知道

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

然而,ActionLink的输出的实际文本&放大器; 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:

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

我风与:
&LT; EM&gt;我-后标题 - 此处&lt; / EM&GT;

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

任何想法如何做到这一点?

Any idea how to do this?

推荐答案

它看起来像ActionLink的始终使用电话HttpUtility.En code上的链接文字。你可以使用UrlHelper生成href和建立锚标记自己。

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>

另外,您可以在德code传递给ActionLink的字符串。构建HTML中的链接似乎稍微更可读(对我) - 尤其是在剃刀。下面是等效比较

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", ...)

这篇关于如何在MVC中使用Html.ActionLink当我绕过HTML编码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆