HTML使用RazorEngine呈现为文字字符串。 我怎么能阻止这个?

我正在尝试使用RazorEngine( http://razorengine.codeplex.com/ )生成HTML文档。 一切都在工作,但我现在的问题是一些HTML正在被正确呈现,而我嵌套在其中的HTML被呈现为文字HTML,所以而不是浏览器按预期显示表和div,它是显示例如

"
"

我通过调用以下内容启动此过程:

 string completeHTML = RazorEngine.Razor.Parse("InstallationTemplate.cshtml", new { Data = viewModels }); 

然后将completeHTML写入文件。

“InstallationTemplate.cshtml”定义为:

 @{ var installationReport = new InstallationReport(Model.Data); }     
@installationReport.DigiChannels()

其中InstallationReportDigiChannels的定义如下:

 public static class InstallationReportExtensions { public static string DigiChannels(this InstallationReport installationReport) { return installationReport.GetDigiChannelsHtml(); } } public class InstallationReport { public string GetDigiChannelsHtml() { // the following renders the template correctly string renderedHtml = RazorReport.GetHtml("DigiChannels.cshtml", GetDigiChannelData()); return renderedHtml; } } public static string GetHtml(string templateName, object data) { var templateString = GetTemplateString(templateName); return RazorEngine.Razor.Parse(templateString, data); } 

GetDigiChannelsHtml()运行并返回renderedHtml ,执行行将返回到TemplateBase.cs到方法ITemplate.Run(ExecuteContext context) ,该方法定义为:

  string ITemplate.Run(ExecuteContext context) { _context = context; var builder = new StringBuilder(); using (var writer = new StringWriter(builder)) { _context.CurrentWriter = writer; Execute(); // this is where my stuff gets called _context.CurrentWriter = null; } if (Layout != null) { // Get the layout template. var layout = ResolveLayout(Layout); // Push the current body instance onto the stack for later execution. var body = new TemplateWriter(tw => tw.Write(builder.ToString())); context.PushBody(body); return layout.Run(context); } return builder.ToString(); } 

当我检查builder.ToString() ,我可以看到它包含适用于InstallationTemplate.cshtml内容的HTML,并为DigiChannels.cshtml内容转义了HTML。 例如:

在此处输入图像描述

如何让@installationReport.DigiChannels()包含正确的HTML而不是它当前正在执行的转义HTML?

你有没有尝试过:

 @Raw(installationReport.DigiChannels()) 

编辑:我可以按照以下方式使用它(MVC3)

 @Html.Raw(installationReport.DigiChannels()) 

@Raw的替代@Raw是更改您的API以在适当的位置返回HtmlString

表示不应再次编码的HTML编码字符串。

默认的剃刀行为是对string s进行编码。