HTML正在使用RazorEngine呈现为文字字符串。我怎样才能避免这种情况? [英] HTML is being rendered as literal string using RazorEngine. How can I prevent this?

查看:1186
本文介绍了HTML正在使用RazorEngine呈现为文字字符串。我怎样才能避免这种情况?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想生成RazorEngine( http://razorengine.codeplex.com/)。一切的大多的工作,但我现在的问题是,一些被正确呈现的HTML,和HTML,我已经嵌套在所呈现的文字HTML,因此而不是浏览器的显示表&安培; div的预期,其显示例如

I'm trying to generate a HTML document with RazorEngine (http://razorengine.codeplex.com/). Everything is mostly working, but the problem I have now is some of the HTML is being rendered correctly, and HTML that I have nested in that is being rendered as literal HTML, so rather than the browser displaying the tables & divs as expected, it's displaying e.g.

 "<table></table><div></div>"



我致电以下揭开序幕这个过程:

I kick off this process by calling the following:

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



completeHTML ,然后写入到文件

InstallationTemplate.cshtml被定义为:

"InstallationTemplate.cshtml" is defined as:

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

<!DOCTYPE html>
<html>
    <head></head>
    <body>        
        <div>
            <!-- I would expect this to write the rendered HTML
                 in place of "@installationReport.DigiChannels()" -->
            @installationReport.DigiChannels()    
        </div>
    </body>
</html>



其中, InstallationReport DigiChannels 定义如下:

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上下文),其定义为:

After GetDigiChannelsHtml() runs and returns renderedHtml, the line of execution returns to TemplateBase.cs into the method ITemplate.Run(ExecuteContext context), which is defined as:

    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和转义HTML的 DigiChannels.cshtml 的东西。例如:

When I inspect builder.ToString(), I can see it contains proper HTML for the InstallationTemplate.cshtml stuff, and escaped HTML for the DigiChannels.cshtml stuff. For example:

我如何获得 @ installationReport.DigiChannels()来包括正确的HTML,而不是转义HTML,它是目前在做?

How can I get @installationReport.DigiChannels() to include the correct HTML instead of the escaped HTML that it's currently doing?

推荐答案

你试过:

@Raw(installationReport.DigiChannels())

编辑:我可以在下面使用方式(MVC3)

Edit : I could use it in following way (MVC3)

@Html.Raw(installationReport.DigiChannels())

这篇关于HTML正在使用RazorEngine呈现为文字字符串。我怎样才能避免这种情况?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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