且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

是否可以在 ASP.NET MVC 3 中显示数据库中的原始 Html?

更新时间:2022-06-24 02:53:29

您只需要:@Html.Raw(yourEncodedHtmlFromYouDatabase)

我假设数据库中的 html 已经过适当清理(或至少来自可靠来源),因为如果没有,您可能会面临跨站点脚本攻击.

I'm assuming that the html in the database has been properly sanitized (or at least from a reliable source), because if not, you could be opening yourself up to cross-site scripting attacks.

您的方法不起作用的原因是默认情况下 Razor HTML 编码输出(每次您使用 @ 显示某些内容时).Html.Raw 告诉 Razor 您信任 HTML,并且希望在不对其进行编码的情况下显示它(因为它已经是原始 HTML).

The reason your approach didn't work is that Razor HTML-encodes output by default (every time you use @ to display something). Html.Raw tells Razor that you trust the HTML and you want to display it without encoding it (as it's already raw HTML).