RazorEngine @Html 助手工作,但逃避 Html [英] RazorEngine @Html helpers work, but escape the Html

查看:43
本文介绍了RazorEngine @Html 助手工作,但逃避 Html的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 RazorEngine 从网页上的 html 片段中解析模板.(这是一个遗留系统,无法切换到 Mvc Razor 视图,因此我们将小部分切换到使用 RazorEngine 有意义的地方).在 SO 和互联网上有很多问题试图让 Mvc 的 Html 和 Url 助手与 Razor 引擎一起工作.为了使 @Html 语法正常工作,我修改了在 here 中找到的一些代码 将 Html 添加到基本模板:

I am using RazorEngine to parse templates from html snippets on a web page. (This is a legacy system that switching to Mvc Razor views isn't possible, so we are switching small sections over to using RazorEngine where it makes sense). There are many of questions on SO and the internet trying to get Mvc's Html and Url helpers to work with Razor engine. To get @Html syntax to work, I've modified some code found here to add Html to the base template:

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

在对 Html 源代码进行大量调试后,我想我已经成功地实例化了 Html 助手所需的一切,并且它为 @Html.Label 成功运行......问题是生成的 html 是:

After a lot of debugging into the Html source code I think I've managed to instantiate everything that the Html helper needs, and it runs successfully for @Html.Label... The problem is that the resulting html is:

&lt;label for=&quot;MyNumber&quot;&gt;MyNumber&lt;/label&gt;

显然应该是:

<label for="MyNumber">MyNumber</label>

我不知道如何解决这个问题.在查看 RazorEngine 源代码时,我无法找到编码是如何发生的.我最初的想法是 TextWriter 必须对值进行编码,但我无法确认这一点.如何让 @Html.BlahFor() 呈现未转义的 html?

I am stumped as to how to fix this. I was not able to find how the encoding happens when looking through the RazorEngine source. My initial thought was that the TextWriter must be encoding the value but I have not been able to confirm this. How can I get @Html.BlahFor() to render un-escaped html?

推荐答案

我找到了解决我面临的问题的方法.如果 RazorEngine 的基础模板没有转换为 IEncodedString,它将自动对字符串(或对象)进行编码.就我而言,我通过覆盖模板类中的 WriteTo 方法解决了这个问题:

I have found a workaround for the problem I was facing. RazorEngine's base Template will automatically encode a string (or object) if it doesn't cast to an IEncodedString. In my case, I solved this issue by overriding the WriteTo method in my template class:

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

    if (value == null) return;

    var encodedString = value as IEncodedString;
    if (encodedString != null)
    {
        writer.Write(encodedString);
    }
    else
    {
        var htmlString = value as IHtmlString;
        if(htmlString != null) 
            writer.Write(htmlString.ToHtmlString());
        else
        {
            //This was the base template's implementation:
            encodedString = TemplateService.EncodedStringFactory.CreateEncodedString(value);
            writer.Write(encodedString);
        }
    }
}

我还没有看到这个改变是否会导致任何错误,但现在我发现了这个方法,以后应该比较容易改变.

I have yet to see if this change will cause any errors, but now that I found the method it should be relatively easy to change in the future.

检查看到 Html helpers 返回一个 MvcHtmlString,它实现了 IHtmlString,因此通过向该接口添加强制转换,我们可以避免对 helpers 返回的 html 进行编码,同时仍然具有安全性适合此方法的任何其他调用者.

Checked to see Html helpers return an MvcHtmlString, which implements IHtmlString, so by adding a cast to this interface, we can avoid encoding the html returned by the helpers, while still having a safety in place for any other callers of this method.

这篇关于RazorEngine @Html 助手工作,但逃避 Html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆