& 符号被 HTML 编码为 &。有什么办法可以防止这种情况吗?
我有以下代码块,可以为公司创建操作链接。
Html.ActionLink(Html.Encode(currentCompany.GetDisplayName()), "CompanyFactsheet", "Company",
new {friendlyURL = currentCompany.FriendlyURL.ToLower()}, null)%>
一些公司名称包含“&”,这被呈现为 &
有没有办法可以对公司名称进行 Html.Encode
,但是仍然保留 &
符号,因为它应该看起来,而不是 &
?
I have the following block of code that creates an Action Link for a company.
Html.ActionLink(Html.Encode(currentCompany.GetDisplayName()), "CompanyFactsheet", "Company",
new {friendlyURL = currentCompany.FriendlyURL.ToLower()}, null)%>
Some of the company names contain '&', and this is being rendered as &
Is there a way that I can Html.Encode
the company names, but still keep the &
symbol as it's supposed to look, rather than &
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 ActionLink 辅助方法时似乎不需要编码。它在内部编码:
在Mvc中使用Html.ActionLink时如何绕过HTML编码?
It seems like you don't need to encode when using ActionLink helper method. It encodes internally:
How do I bypass the HTML encoding when using Html.ActionLink in Mvc?
仔细检查正在生成的标记。
&
在页面上显示的唯一方法是,如果您对 & 符号进行双重编码(因此页面的源代码将显示& ;
)。这可能是由于存储已经 HTML 编码的字符或您使用
<%:
(HTML 自动为您编码所有内容)而不是<%=
造成的在你的视野中。Double check the markup that is getting generated. The only way that the
&
should be showing on the page is if you're double encoding the ampersand character (so the source of the page would be showing&
).This could be caused by either storing the character already HTML encoded or you're using
<%:
(which HTML encodes everything for you automatically) instead of<%=
in your View.替换 &在保存它们之前将它们替换为其他字符,并在编码后将它们替换回来(最有可能使用jquery)...我知道这听起来有点矫枉过正,但如果你真的需要 Html.Encode (或 <%:, 或 Razor 中的 @ ),这是最好的方法恕我直言......
Replace & to some other charachter before saving them, and replace them back AFTER encoding (using jquery most likely)... I know it sounds like overkill, but if you really need the Html.Encode (or <%:, or @ in Razor), this is the best way IMHO....