Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
501 views
in Technique[技术] by (71.8m points)

c# - HTML is being rendered as literal string using RazorEngine. How can I prevent this?

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

The completeHTML is then written to a file.

"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>

Where InstallationReport and DigiChannels are defined as follows:

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

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

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:

enter image description here

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Have you tried:

@Raw(installationReport.DigiChannels())

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

@Html.Raw(installationReport.DigiChannels())

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...