public async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req, [SendGrid] IAsyncCollector <SendGridMessage> messageCollector, ILogger log, CancellationToken cancellationToken) { string name = req.Query["name"]; string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); dynamic data = JsonConvert.DeserializeObject(requestBody); name = name ?? data?.name; var message = new SendGridMessage(); message.SetFrom("*****@*****.**"); message.AddTo("*****@*****.**"); message.SetSubject("Subject"); var model = new SampleEmailTemplateModel() { Name = name }; var htmlContent = await razorViewToStringRenderer.RenderViewToStringAsync(model); message.AddContent(MimeType.Html, htmlContent); await messageCollector.AddAsync(message, cancellationToken); log.LogInformation("C# HTTP trigger function processed a request."); return(new OkObjectResult(htmlContent)); }
static void Main(string[] args) { // Setup an MVC-style ViewModel var templateModel = new SampleEmailTemplateModel { EmailTagline = "Markup Generated With the Razor Engine !" }; templateModel.ListCollectionItems.Add(new SampleEmailTemplateModelCollectionItem { CollectionItemDescription = "This is the first item (we can show a collection of data)" }); templateModel.ListCollectionItems.Add(new SampleEmailTemplateModelCollectionItem { CollectionItemDescription = "This is the second item (so our demo is demonstrating iteration)" }); // define some comfiguration items used by the dynamic assembly string dynamicAssemblyNamespace = "EmailTemplateDynamicAssembly"; string dynamicDllName = "EmailTemplateDynamicAssemblyDllName"; // dynamically compile a template assembly Assembly templateAssembly = RazorEngineDynamicCompilerHelper <SampleEmailTemplateModel> .CompileTemplate("SampleEmailTemplate.cshtml", dynamicAssemblyNamespace, dynamicDllName); // use the compiled assembly, merging with a populated ViewModel and emitting to console. Console.Write(RazorEngineTemplateHelper <SampleEmailTemplateModel> .MergeViewModelReturnMarkup(templateModel, templateAssembly, dynamicAssemblyNamespace)); Console.ReadKey(); }
public async Task <IActionResult> GenerateFromTemplate() { var templateModel = new SampleEmailTemplateModel(); templateModel.EmailHeader = "Any old text"; templateModel.ListCollectionItems.Add(new SampleEmailTemplateModelCollectionItem { CollectionItemDescription = "CollectionItemOne" }); templateModel.ListCollectionItems.Add(new SampleEmailTemplateModelCollectionItem { CollectionItemDescription = "CollectionItemTwo" }); // define some comfiguration items used by the dynamic assembly string dynamicAssemblyNamespace = "EmailTemplateDynamicAssembly"; string dynamicDllName = "EmailTemplateDynamicAssemblyDllName"; // dynamically compile a template assembly Assembly templateAssembly = RazorEngineDynamicCompilerHelper <SampleEmailTemplateModel> .CompileTemplate("SampleEmailTemplate.cshtml", dynamicAssemblyNamespace, dynamicDllName); var renderedHtml = RazorEngineTemplateHelper <SampleEmailTemplateModel> .MergeViewModelReturnMarkup(templateModel, templateAssembly, dynamicAssemblyNamespace); return(Ok(renderedHtml)); }