我使用RazorEngine.Parse对RazorEngine电子邮件重用了相同的部分,但当我在常规视图中使用相同的部分时,new RazorEngine.Text.RawString不起作用,也不会忽略HTML。我无法使用Html.Raw,因为RazorEngine无法读取它。我怎么才能避免这个问题呢?
<p>
@(new RazorEngine.Text.RawString(Model.Body))
</p>在常规的asp.net mvc剃刀视图中显示底部标记。
<p>
Welcome!<br/><br/>Body
</p>发布于 2018-10-18 04:26:13
您可以在创建RazorEngineService实例时指定ITemplateServiceConfiguration,如项目的git repository中所示。
库中的代码:
/// <summary>
/// A simple helper demonstrating the @Html.Raw
/// </summary>
public class MyHtmlHelper
{
/// <summary>
/// A simple helper demonstrating the @Html.Raw
/// </summary>
public IEncodedString Raw(string rawString)
{
return new RawString(rawString);
}
}
/// <summary>
/// A simple helper demonstrating the @Html.Raw
/// </summary>
public abstract class MyClassImplementingTemplateBase<T> : TemplateBase<T>
{
/// <summary>
/// A simple helper demonstrating the @Html.Raw
/// </summary>
public MyClassImplementingTemplateBase()
{
Html = new MyHtmlHelper();
}
/// <summary>
/// A simple helper demonstrating the @Html.Raw
/// </summary>
public MyHtmlHelper Html { get; set; }
}使用:
class Program
{
static void Main(string[] args)
{
var config = new TemplateServiceConfiguration();
config.BaseTemplateType = typeof(MyClassImplementingTemplateBase<>);
using (var service = RazorEngineService.Create(config))
{
string template = "<p>@Html.Raw(Model.Body)</p>";
var result = service.RunCompile(template, "templateKey", null, new { Body = "Welcome!<br /><br /><Label>Hello</label>" });
Console.WriteLine(result);
}
Console.ReadLine();
}
}您唯一需要记住的就是在创建RazorEngineService实例时提供ITemplateServiceConfiguration对象。
P.S: @(new RazorEngine.Text.RawString(Model.Body))在部分视图中不起作用,因为它被包装在@()周围,并且指令中的任何字符串都将在写入输出流之前进行编码。
https://stackoverflow.com/questions/52433559
复制相似问题