我如何告诉浏览器渲染字符串,而不是显示它

发布于 2024-10-07 05:39:51 字数 759 浏览 8 评论 0原文

我创建了这个扩展方法,它以超链接的形式返回数据库中所有类别的字符串。伟大的!

@Html.MyMenu()

问题在于链接显示为文本而不是呈现为超链接。

查看源代码时,我看到:

<div id="menucontainer">

                &lt;a href=&quot;/Anuncio/Electronics&quot;&gt;Electronics&lt;/a&gt;&lt;a href=&quot;/Anuncio/Clothes&quot;&gt;Clothes&lt;/a&gt;&lt;a href=&quot;/Anuncio/Domestic&quot;&gt;Domestic&lt;/a&gt;&lt;a href=&quot;/Anuncio/Garden&quot;&gt;Garden&lt;/a&gt;

            </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 技术交流群。

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

发布评论

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

评论(4

你爱我像她 2024-10-14 05:39:51

Razor 默认转义 HTML。

为了避免这种情况,请执行以下操作:

编写/输出 HTML未转义的字符串

Razor escapes HTML by default.

To avoid that, please do something like this:

Writing/outputting HTML strings unescaped

梦里°也失望 2024-10-14 05:39:51

在 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.

你的笑 2024-10-14 05:39:51

那么您的扩展方法应该返回一个 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.

尝蛊 2024-10-14 05:39:51

输出原始 HTML

RC2 支持 @Html.Raw()从 Scott Guthrie 的 RC2 公告

在 RC2 中,我们添加了一个 Html.Raw() 辅助方法,您可以使用该方法明确指示您不想对输出进行 HTML 编码,而是希望“按原样”呈现内容

RC2 supports @Html.Raw() to output raw HTML

From Scott Guthrie's RC2 anouncement

With RC2 we are adding a Html.Raw() helper method that you can use to explicitly indicate that you do not want to HTML encode your output, and instead want to render the content “as-is”

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