RazorEngine: cannot use Html.Raw

前端 未结 2 997
慢半拍i
慢半拍i 2020-12-06 16:29

using RazorEngine outside asp.net I\'m experiencing this error when I try to write raw html by using @Html.Raw(\"html string here\"):

Una

2条回答
  •  醉酒成梦
    2020-12-06 16:41

    I implemented my own Raw whose result implements both IHtmlString and IEncodedString... and it worked! :)

    In my csthml:
    @MyRazorParser.Raw("Testing")
    

    This works both when MVC uses it and when the RazorEngine parser uses it.

    public class MyRawResult : RazorEngine.Text.IEncodedString, System.Web.IHtmlString
    {
        public string Value;
        public MyRawResult(string value) { Value = value; }
        public string ToEncodedString()
        {
            return Value;
        }
    
        public string ToHtmlString()
        {
            return Value;
        }
    
        public override string ToString()
        {
            return Value;
        }
    }
    
    public static class MyRazorParser
    {
        public static object Raw(string str)
        {
            return new MyRawResult(str);
        }
    }
    

提交回复
热议问题