RazorEngine issues with @Html

Check https://github.com/Antaris/RazorEngine/wiki/6.-Encoding-Values page. I copy / past it here:

By default, RazorEngine is configured to encode as HTML. This sometimes presents problems where certain characters are encoded as HTML when you wanted the output to be as-is.

To output something in raw format, use the @Raw() built-in method as shown in the following example:

string template = "@Raw(Model.Data)";
var model = new { Data = "My raw double quotes appears here \"hello!\"" };

string result = Razor.Parse(template, model);

Which should result in:

My raw double quotes appears here "hello!"

The Html and Url helper properties are actual features of MVC's implementation of Razor in their view engine. Out of the box, Html and Url are not currently supported without specialising a custom base template.

The upcoming v3 release will be accompanied by an associated RazorEngine.Web release, which will hopefully include an MVC3 compatible base template with Html and Url support.

The example I wrote on the project homepage, is purely an example of using a custom base template.

You can find out more about v3 at https://github.com/Antaris/RazorEngine


This is over a year old, but since I haven't found a working copy anywhere on the internet and the github page is inactive, I figured I would share my implementation to add @Html helper syntax to RazorEngine. Here is the implementation I ended up with, using Abu Haider's implementation as a starting point.

Courtesy of miketrash's comment: If you are trying to use @Html.Action(), you will need to add the RequestContext (you can use HttpContext.Current.Request.RequestContext). I did not include request context because it is not always available for my application.

[RequireNamespaces("System.Web.Mvc.Html")]
public class HtmlTemplateBase<T>:TemplateBase<T>, IViewDataContainer
{
    private HtmlHelper<T> helper = null;
    private ViewDataDictionary viewdata = null;       

    public HtmlHelper<T> Html
    {
        get
        {
            if (helper == null) 
            {                  
                var writer = this.CurrentWriter; //TemplateBase.CurrentWriter
                var vcontext = new ViewContext() { Writer = writer, ViewData = this.ViewData};

                helper = new HtmlHelper<T>(vcontext, this);
            }
            return helper;
        }
    }

    public ViewDataDictionary ViewData
    {
        get
        {
            if (viewdata == null)
            {
                viewdata = new ViewDataDictionary();
                viewdata.TemplateInfo = new TemplateInfo() { HtmlFieldPrefix = string.Empty };

                if (this.Model != null)
                {
                    viewdata.Model = Model;
                }
            }
            return viewdata;
        }
        set
        {
            viewdata = value;
        }
    }

    public override void WriteTo(TextWriter writer, object value)
    {
        if (writer == null)
            throw new ArgumentNullException("writer");

        if (value == null) return;

        //try to cast to RazorEngine IEncodedString
        var encodedString = value as IEncodedString;
        if (encodedString != null)
        {
            writer.Write(encodedString);
        }
        else
        {
            //try to cast to IHtmlString (Could be returned by Mvc Html helper methods)
            var htmlString = value as IHtmlString;
            if (htmlString != null) writer.Write(htmlString.ToHtmlString());
            else
            {
                //default implementation is to convert to RazorEngine encoded string
                encodedString = TemplateService.EncodedStringFactory.CreateEncodedString(value);
                writer.Write(encodedString);
            }

        }
    }
}

I also had to override the WriteTo method of TemplateBase, because otherwise RazorEngine will html-encode the result of the helper method meaning you'll escape the '<', '>', and quotes (see this question). The override adds a check for the value being an IHtmlString before resorting to performing an encoding.

Tags:

Razorengine