RazorEngine: cannot use Html.Raw

微笑、不失礼 提交于 2019-12-17 19:57:19

问题


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

Unable to compile template. The name 'Html' does not exist in the current context

Can you help me?

Thanks!


回答1:


The solution has been found here: https://github.com/Antaris/RazorEngine/issues/34

It's enough to use @(new RawString("html string here")) or @Raw("html string here") instead of @Html.Raw("html string here").

I hope this helps! Bye




回答2:


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

In my csthml:
@MyRazorParser.Raw("<b>Testing</b>")

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);
    }
}


来源:https://stackoverflow.com/questions/23603593/razorengine-cannot-use-html-raw

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