'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 1:[1]

Using RenderFragment :

@page "/"

<h1>Hello, world!</h1>

Welcome to your new app.

@StringToHtml("Hello<h1>text</h1>")

@code {

    RenderFragment StringToHtml(string htmlString)
    {
        return new RenderFragment(b => b.AddMarkupContent(0, htmlString));
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Aziz Gulakov