Is there an equivalent to Html.Raw in Blazor?

一个人想着一个人 提交于 2020-06-10 07:20:18

问题


I have some HTML that is stored in a string. How can I render this is a Blazor/Razor view without automatic HTML encoding?


回答1:


Feature to render raw HTML was added in Blazor 0.5.0 version. This is the example of how raw HTML can be rendered from string containing HTML content:

@((MarkupString)myMarkup)

@functions {
    string myMarkup = "<p class='markup'>This is a <em>markup string</em>.</p>";
}

More info can be found in "Blazor 0.5.0 experimental release now available" announcement.




回答2:


Not right now, but will have it probably in the next version: Follow this

Workaround (from that issue):

cshtml

    

    @functions{
        [Parameter] string Content { get; set; }
        private ElementRef Span;

        protected override void OnAfterRender()
        {
            Microsoft.AspNetCore.Blazor.Browser.Interop.RegisteredFunction.Invoke("RawHtml", Span, Content);
        }
    }

index.html

        
            Blazor.registerFunction('RawHtml', function (element, value) {
                element.innerHTML = value;
                for (var i = element.childNodes.length - 1; i >= 0; i--) {
                    var childNode = element.childNodes[i];
                    element.parentNode.insertBefore(childNode, element);
                }
                element.parentNode.removeChild(element);
                return true;
            });
        



来源:https://stackoverflow.com/questions/50604366/is-there-an-equivalent-to-html-raw-in-blazor

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!