我如何告诉浏览器渲染字符串,而不是显示它
我创建了这个扩展方法,它以超链接的形式返回数据库中所有类别的字符串。伟大的!
@Html.MyMenu()
问题在于链接显示为文本而不是呈现为超链接。
查看源代码时,我看到:
<div id="menucontainer">
<a href="/Anuncio/Electronics">Electronics</a><a href="/Anuncio/Clothes">Clothes</a><a href="/Anuncio/Domestic">Domestic</a><a href="/Anuncio/Garden">Garden</a>
</div>
我想我可能是错的,但我记得在 MVC2(使用默认视图引擎)中你有:
<%: this is rendered, right? %>
或者我错了?无论如何,我正在使用 MVC3 和 Razor 引擎。非常感谢你们的帮助。我真的很喜欢尽可能多地学习这方面的知识。
I have this extension method I created and it returns a string of all the categories in my database as hyperlinks. Great!
@Html.MyMenu()
The problem is that the links are being displayed as text and not rendered as hyperlinks.
When viewing the source code I see:
<div id="menucontainer">
<a href="/Anuncio/Electronics">Electronics</a><a href="/Anuncio/Clothes">Clothes</a><a href="/Anuncio/Domestic">Domestic</a><a href="/Anuncio/Garden">Garden</a>
</div>
I think I may be wrong but I remember that in MVC2 (using the default view engine) you had:
<%: this is rendered, right? %>
Or am I mistaken? Anyways, I'm using MVC3 and the Razor engine. Thanks a lot for your help guys. I'm really enjoying learning as much as I can about this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Razor 默认转义 HTML。
为了避免这种情况,请执行以下操作:
编写/输出 HTML未转义的字符串
Razor escapes HTML by default.
To avoid that, please do something like this:
Writing/outputting HTML strings unescaped
在 RC2 中,一个名为 @HTml.Raw 的新方法应该可以实现这一点。
或者您可以更改 MyMenu 以返回 HtmlString 或 MvcString 而不仅仅是字符串。
In RC2 a new method called @HTml.Raw should to this.
Or you can change MyMenu to return HtmlString or MvcString rather than just string.
那么您的扩展方法应该返回一个
MvcHtmlString
以便使用<%: %>
在您的页面上正确显示(如果它返回一个字符串,则所有尖括号和其他 html 特殊字符将被 html 编码。well your extension method should be returning an
MvcHtmlString
in order to properly display on your page using the<%: %>
If it returns a string, all angle brackets and other html special characters will be html-encoded.输出原始 HTML
RC2 支持 @Html.Raw()从 Scott Guthrie 的 RC2 公告
RC2 supports @Html.Raw() to output raw HTML
From Scott Guthrie's RC2 anouncement