简体   繁体   中英

How to render invalid HTML tags as text alongside valid HTML tags?

I have an HTML string in my code behind which has some valid html tags such as <br/> , <p></p> , and some invalid tags such as <test> . I want to render both sets of tags in the browser in such a way that invalid tags are rendered as plain text.

For example, the string

<test><br/>hi mark.<br/>how are you.My email is <test@test.com>

would to need to output on the browser as

<test> hi mark.how are you.My email is <test@test.com>

You would need to whitelist the elements you consider to be valid, and then encode those which do not match the whitelist so that they are instead converted to &lt;test&gt; as opposed to <test> . The ampersand values will render as < text on the page.

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