如何把razor生成的html变成字符串?

How to get html generated by razor into a string?

我使用 umbraco 创建了一个工作流,我想做的是: 创建一个工作流程,以获取由剃刀模板 (umbraco.forms) 创建的 html。

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

}

我尝试了 string template = html.Raw(Template); 但我无法访问 html.Raw("");

我已经尝试了 this 解决方案,但似乎 record.ParseWithRazorView(filePath); 不存在

有一个工作流程可以做一些接近的事情,但我似乎无法重写或访问此代码

如果你不明白我假装的是什么,请发表评论,我会更新问题的所有细节

我最近想发送 html 电子邮件并从剃刀页面获取 html-conten。我成功了: https://github.com/aspnet/Entropy/blob/master/samples/Mvc.RenderViewToString/RazorViewToStringRenderer.cs

我还允许您将模型注入视图,这样您就可以动态更改呈现的 html。

然后您可以这样调用它:

var html = await myRazorViewToStringRenderer.RenderViewToStringAsync("path/to/view.cshtml", myViewModel);

我试过 RazorLight,这里是代码片段。

private async Task<string> GetMailBody(string teamplteName, object mailObject)
{
    var engine = new RazorLightEngineBuilder()
      .UseFilesystemProject("root path")
      .UseMemoryCachingProvider()
      .Build();

    string result = await engine.CompileRenderAsync(teamplteName, mailObject);

    return result;
}

创建此方法:

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

并在每个你想要导出 razor 模板字符串的地方调用它。

viewName 是您的模板 name.model 是您的数据,如果模板需要的话。

在您的代码创建助手中,它将生成您的模板。

@helper TemplateGenerator()
{
//generate your template here
}

然后你可以编写这段代码

public HtmlString getTemplateHtml(HelperResult Template)
{
HtmlString str = Template.ToString(); 
}

在这个例子中我使用了 HtmlString,但是你可以试试字符串,不知道结果如何。

在 razor 中你应该这样调用你的函数:

@GetTemplateHtml(TemplateGenerator()); 

这对我来说很好用。不确定这是最好的解决方案,但我希望它能有所帮助

看看类似 Postal 的东西,它允许您使用视图创建和发送电子邮件,您可以将自己的模型等传递到视图中。

我创建了一个视图和一个 umbraco controller

namespace name.Core.Controllers
{
    public class TemplateController : Umbraco.Web.Mvc.SurfaceController
    {
        public ActionResult Template()
        {

            return View();
        }
   }
}

我把URL传给"pathtoTemplate"

 public static class TemplateHTML
    {
        public static string GetTemplate(string pathtoTemplate)
        {
            using (WebClient client = new WebClient())
            {
                var encodingbody = client.DownloadData(CaminhoTemplate);
                return Encoding.UTF8.GetString(encodingbody);
            }
        }
    }

var body = TemplateHTML.GetTemplate("http://example.com/umbraco/surface/Template/Template")