blazorblazor-client-side

Convert plain text with html tags to html string and render it in Blazor


Sample:

@{
     var s = "<p>Sample text</p>";
 }

Expectation:

Sample text.

I want it rendered on browser but I couldn't render it. It just outputs the whole value of s as text string. I already try

@(new HtmlString(s))

encoded it with HttpUtility.HtmlEncode and decode it with HttpUtility.HtmlDecode but still no use.


Solution

  • You will need

    <div >@((MarkupString)s)</div>
    
    @code
    {
        string s = "<p>Sample text</p>";
    }
    

    The <p> will render inside the <div>