RazorEngine.Text.RawString not working on regular MVC partial view
Asked Answered
B

1

10

I reuse the same partial for a RazorEngine email using RazorEngine.Parse, but when I use the same Partial in a regular view new RazorEngine.Text.RawString doesnt work and doesnt ignore HTML. I cant use Html.Raw because the RazorEngine cant read it. How can I get around this?

<p>
    @(new RazorEngine.Text.RawString(Model.Body))
</p>

Shows the bottom markup in a regular asp.net mvc razor view.

<p>
   Welcome!&lt;br/&gt;&lt;br/&gt;Body
</p>
Burris answered 20/9, 2018 at 21:27 Comment(4)
Not sure what is your question. According to the code of RawString, that is exactly what should be shown in your razor view. RawString. That class has a method called ToEncodedString that "should" return the encoded string, according to its doc, but it just returns the same value passed in the constructor. And that method is the one called in the ToString() method. github.com/Antaris/RazorEngine/blob/master/src/source/…Pringle
It doesnt work in a regular asp.net mvc view/partial. It shows the <br/> as text, not HTML. It's like I can reuse this markup for regular views, only for views used by this Email engine. It literally show <br/><br/> in the web browser. I updated the text.Burris
By looking at the implementation of RawString, that is just what it does, maybe this should be a question to post in RazorEngine's Github Issues?Pringle
I did and been waiting for response.Burris
C
3

You can specify ITemplateServiceConfiguration when creating an instance of RazorEngineService as shown in project's git repository.

Code from the 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; }
}

Usage:

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

Only thing you need to remember is to provide ITemplateServiceConfiguration object when creating an instance of RazorEngineService.

P.S: @(new RazorEngine.Text.RawString(Model.Body)) is not working in partial view because it is wrapped around @() and any string from the directive will be encoded before it is written to the output stream.

Circum answered 17/10, 2018 at 20:26 Comment(7)
If its not working do you have a better option for the wrapped parenthesis?Burris
@MikeFlynn Not really.Circum
Really? There is no better way?Burris
@MikeFlynn Not that I know of. Well, the problem is you want your code to work with two different systems. Solution for one is not compatible with another one so you will have to work with some limitations, right?Circum
Yes well I have copied content in two different areas and I just can’t believe there isn’t a better way then the templating one you gaveBurris
@MikeFlynn Sure, I would love know to find out about better solution. What I have posted here is from the project's github repo.Circum
Been voted up 9 times now, look like others want a legit solution tooBurris

© 2022 - 2024 — McMap. All rights reserved.