简体   繁体   中英

ASP.NET MVC4 - display HTML containing string as raw HTML

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 (ie &lt;SPAN&gt; 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()

You need to use @Html.Raw :

@Html.Raw("<h1>Header</h1>")

Will output the text Header .

试试这个辅助方法

@Html.Raw(myStr)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM