ASP.NET MVC4 - 包含字符串显示HTML作为原始的HTML [英] ASP.NET MVC4 - display HTML containing string as raw HTML
本文介绍了ASP.NET MVC4 - 包含字符串显示HTML作为原始的HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个字符串,从数据库中读取,包含我要输出HTML。尽管应用 HttpUtility.HtmlDe code()
,查看始终呈现字符串作为连接codeD HTML(即&放大器; LT; SPAN&放大器; GT;
而不是< SPAN方式>
)
我使用的:
字符串测试= WebUtility.HtmlDe code(myStr中);
<跨度> @测试< / SPAN>
我曾尝试:
字符串测试= HttpUtility.HtmlDe code(myStr中);
<跨度> @测试< / SPAN><跨度> @ HttpUtility.HtmlDe code(myStr中)LT; / SPAN>
解决方案
使用的 Html.Raw()
@ Html.Raw(<跨度>您好< / SPAN>中)
所有的助手和剃刀等元素的输出是通过 HttpUtility.HtmlEn code
放,除非他们实施 IHtmlString
。但在这里你最好的选择是使用 Html.Raw()
I have a string, read from a database, that contains HTML that I want to output. Despite applying HttpUtility.HtmlDecode()
, the View always renders the string as encoded HTML (i.e. <SPAN>
instead of <SPAN>
).
I am using:
string test = WebUtility.HtmlDecode(myStr);
<span>@test</span>
I have tried:
string test = HttpUtility.HtmlDecode(myStr);
<span>@test</span>
<span>@HttpUtility.HtmlDecode(myStr)</span>
解决方案
Use Html.Raw()
@Html.Raw("<span>Hello</span>")
All the output from helpers and other elements in Razor are put through HttpUtility.HtmlEncode
, unless they implement IHtmlString
. But your best option here is using Html.Raw()
这篇关于ASP.NET MVC4 - 包含字符串显示HTML作为原始的HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文