Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to render encoded tags as proper HTML, rather than text?

Tags:

I'm getting the following text from a database: (supplied by client, so I can't do much with it)

investment professionals.<BR /><BR /> blah blah blah

which is getting rendered as:

investment professionals.<BR /><BR /> blah blah blah

I don't want to print the <BR /> tags on the screen. I want them to behave as actual breaks.

The following Html Helper code builds the span it exists in, adds that to a div and returns the HTML string:

StringBuilder sbElements = new StringBuilder();

TagBuilder span = new TagBuilder("span") {InnerHtml = subject.AboutText};
sbElements.Append(span.ToString());

TagBuilder div = new TagBuilder("div");
div.MergeAttribute("class", "about-text");
div.InnerHtml = sbElements.ToString();

return div.ToString();

If I Html.Encode() the output of the helper method, the encoded tags - /&gt;&lt; - get written to the screen. How can I take the source text I have and ensure that the tags get rendered as HTML, rather than text?

like image 603
DaveDev Avatar asked Mar 15 '11 16:03

DaveDev


People also ask

How do you render a string with HTML tags in react?

To render the html string in react, we can use the dangerouslySetInnerHTML attribute which is a react version of dom innerHTML property. The term dangerously is used here to notify you that it will be vulnerable to cross-site scripting attacks (XSS).

How do I render in HTML?

The Render FunctionThe ReactDOM.render() function takes two arguments, HTML code and an HTML element. The purpose of the function is to display the specified HTML code inside the specified HTML element.

What is render tag in HTML?

Rendering tags are text strings embedded in the server response file HTML source code. A rendering tag has this general form: {{ tag }} {{ tag (parameter-list) }}

How do you render text in HTML?

You can show HTML tags as plain text in HTML on a website or webpage by replacing < with &lt; or &60; and > with &gt; or &62; on each HTML tag that you want to be visible. Ordinarily, HTML tags are not visible to the reader on the browser.


3 Answers

If you are using Razor, it will doubly-encode your string as @Chevex described. If you use the new MVC3 <%: %> syntax, it will also doubly-encode it. Regardless of your view engine, you can work around the encoding with either the IHtmlString route (e.g., MvcHtmlString) described by @Chevex or by bypassing the default encoding using a different template syntax.

The later, which doesn't involve changing any code, just requires tweaking the syntax you use to render to a view.

For Razor:

@Html.Raw(yourvariable) 

For MVC's default view template system:

<%=yourvariable%> 
like image 122
patridge Avatar answered Oct 03 '22 00:10

patridge


Try this:

    String Input = "investment professionals.&lt;BR /&gt;&lt;BR /&gt; blah blah blah";      String Output = Server.HtmlDecode(Input); 
like image 23
Steve Wellens Avatar answered Oct 03 '22 01:10

Steve Wellens


Change return div.ToString() to MvcHtmlString.Create(div.ToString()) and the method return type to MvcHtmlString instead of a string. This will prevent the view engine from automatically encoding the output.

Returning MvcHtmlString is the standard practice for rendering HTML output via helper methods.

What is happening is that you are decoding the value from the database and the view engine is re-encoding it when it is rendered. The Razor view engine automatically HTML encodes output in your views unless it is an MvcHtmlString. Returning MvcHtmlString will stop that from happening.

public static MvcHtmlString MyNonHtmlEncodedOutput(this HtmlHelper html)
{
        StringBuilder sbElements = new StringBuilder();

        TagBuilder span = new TagBuilder("span") {InnerHtml = Server.HtmlDecode(subject.AboutText)};
        sbElements.Append(span.ToString());

        TagBuilder div = new TagBuilder("div");
        div.MergeAttribute("class", "about-text");
        div.InnerHtml = sbElements.ToString();

        return MvcHtmlString.Create(div.ToString());
}
like image 30
Chev Avatar answered Oct 03 '22 01:10

Chev