2

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

3 Answers 3

6

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()

Sign up to request clarification or add additional context in comments.

Comments

3

You need to use @Html.Raw:

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

Will output the text Header.

Comments

2

Try this helper method

@Html.Raw(myStr)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.