简体   繁体   中英

How to display HTML content in C# string?

I am trying to achieve something like this:

string html = @"Hello <b> World ! </b>";

The desired output would be

Hello World !

The string html can be used anywhere on label, textbox, etc.

But it is not working. It just displays as it is Hello <b> World ! </b> Hello <b> World ! </b>

Is there any other way to do this?

尝试HtmlString如:

HtmlString html = new HtmlString("Hello <b> World ! </b>");

Use @Html.Raw()

@Html.Raw(string);

See here for more: http://forums.asp.net/t/1903975.aspx?how+to+use+html+raw

Your output is HTML-encoded, probably, automatically (s. this article http://weblogs.asp.net/scottgu/archive/2010/04/06/new-lt-gt-syntax-for-html-encoding-output-in-asp-net-4-and-asp-net-mvc-2.aspx ). If you use the ASPX rendering, use <%= %> instead of <%: %>.

Depends on the version of ASP.NET, but your safest bet is to create a literal control

<asp:Literal runat='server' id='yourOutput' Text='' />

And then set it on code behind

yourOutput.Text = html;

This should work on all versions of classic ASP.NET - for MVC projects you already got good answers.

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