【问题标题】:RazorEngine: cannot use Html.RawRazorEngine:不能使用 Html.Raw
【发布时间】:2014-06-29 11:52:12
【问题描述】:

在 asp.net 之外使用 RazorEngine 当我尝试使用 @Html.Raw("html string here") 编写原始 html 时遇到此错误:

无法编译模板。当前上下文中不存在名称“Html”

你能帮帮我吗?

谢谢!

【问题讨论】:

  • 为什么这被否决了?这个问题帮助了我。
  • 可能是因为人们认为这是一个标准的剃须刀问题而不是引擎。

标签: asp.net-mvc razor razorengine


【解决方案1】:

已在此处找到解决方案:https://github.com/Antaris/RazorEngine/issues/34

使用@(new RawString("html string here"))@Raw("html string here") 代替@Html.Raw("html string here") 就足够了。

我希望这会有所帮助! 再见

【讨论】:

  • 这些都不再适用于 dotnet 5。
【解决方案2】:

我实现了我自己的 Raw,其结果同时实现了 IHtmlString 和 IEncodedString...并且它成功了! :)

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

这在 MVC 使用它在 RazorEngine 解析器使用它时都有效。

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

【讨论】:

    【解决方案3】:

    就我而言,我在 _ViewImports.cshtml 文件中缺少对以下内容的引用:

    @using Microsoft.AspNetCore.Mvc;
    

    一旦我添加了这个 ff8mania 的答案对我有用。

    【讨论】:

      猜你喜欢
      • 2012-05-12
      • 2015-06-14
      • 1970-01-01
      • 2015-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-10
      • 1970-01-01
      相关资源
      最近更新 更多