Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get html generated by razor into a string?

I created a workflow using umbraco and what I am trying to do is: create a workflow that get the html created by a razor template (umbraco.forms).

Here is the locations where is the templates of umbraco.forms

Views\Partials\Forms\Emails This template is what I want to send by email when this workflow is called.

public class SendMail : WorkflowType
{  
    //Here I get the path where the razor template is
    [Umbraco.Forms.Core.Attributes.Setting("Path to template",
     Description = "Path to template",
     View = "TextField")]
     public string Template{ get; set; }
   public SendMail()
   {

    this.Id = new Guid("ccbeb0d5-adaa-4729-8b4c-4bb439dc0204");
    this.Name = "Send Mail";
    this.Description = "Send Mail";
    this.Icon = "icon-plugin";
    this.Group = "Services";
   }
    //When workflow get called this method is executed 
    public override WorkflowExecutionStatus Execute(Record record, RecordEventArgs e)
   {

      //here I'm trying to get the template converted to string however isn't work
     //  string template = html.Raw(Template).ToString();
   }

}

I tried string template = html.Raw(Template); but I have no access to the html.Raw("");

I've tried this solutions but seems that record.ParseWithRazorView(filePath); doesn't exists

There is a workflow that do something close but seems I can't re-write or get access to this code

enter image description here

  • Email Templates
  • Workflow Types
  • Creating a custom workflow

If you don't understand exactly what I pretend please comment and I will update the question with all details

like image 525
Amadeu Antunes Avatar asked Jan 25 '23 21:01

Amadeu Antunes


2 Answers

I recently wanted to send html emails and get the html-conten from a razor page. I had success with this: https://github.com/aspnet/Entropy/blob/master/samples/Mvc.RenderViewToString/RazorViewToStringRenderer.cs

I also allows you to inject a model into the view, so you can dynamically change the rendered html.

You can then invoke it this way:

var html = await myRazorViewToStringRenderer.RenderViewToStringAsync("path/to/view.cshtml", myViewModel);
like image 54
stipps Avatar answered Jan 28 '23 11:01

stipps


create this method :

public string RenderRazorToString(string viewName, object model, ViewDataDictionary viewData = null)
{
    ViewData.Model = model;

    if (viewData != null)
        foreach (KeyValuePair<string, object> item in viewData)
            ViewData.Add(item);

    using (var sw = new System.IO.StringWriter())
    {
        var viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);
        var viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);
        viewResult.View.Render(viewContext, sw);
        viewResult.ViewEngine.ReleaseView(ControllerContext, viewResult.View);
        return sw.GetStringBuilder().ToString();
    }
}

and call it every where you want string export of razor's template .

viewName is Your Template name.model is your data if the template needs it.

like image 41
masoud Avatar answered Jan 28 '23 11:01

masoud