1

I have prepared a .net core console application (Framework version .Net Core 2.2) for sending email as a service. Right now its working completely fine with static html content being hardcoded into service method for generating email body string. I am in seek of the code which provides me a solution to render a razor view to have a html string with the model data.

Tried to implement the RazorEngine dll in entity framework ver. 4.5. with below code

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GenerateEmailUsingRazor.Model;
using RazorEngine.Templating;

namespace GenerateEmailUsingRazor
{
    class Program
    {
        static readonly string TemplateFolderPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "EmailTemplates");

        static void Main(string[] args)
        {
            var model = GetUserDetail();


            var emailTemplatePath = Path.Combine(TemplateFolderPath, "InviteEmailTemplate.cshtml");

            var templateService = new TemplateService();
            var emailHtmlBody = templateService.Parse(File.ReadAllText(emailTemplatePath), model, null, null);


            Console.WriteLine(emailHtmlBody);

            Console.ReadLine();

        }

        private static UserDetail GetUserDetail()
        {
            var model = new UserDetail()
                            {
                                Id = 1,
                                Name = "Test User",
                                Address = "Dummy Address"
                            };


            for (int i = 1; i <= 10; i++)
            {
                model.PurchasedItems.Add("Item No " + i);
            }
            return model;
        }
    }
}

Expected Result:

Console Application should render the razor view and provide me the resultant html string.

3
  • Possible duplicate of Return View as String in .NET Core Commented Dec 28, 2018 at 13:35
  • it's not wokting code and I think that code for web not console app Commented Dec 28, 2018 at 13:39
  • 1
    I reviewed your issue and would like to suggest an alternate solution to view rendering. You can use the visual studio T4 Text Templates Technique to achieve html string using a predefined template structure. learn.microsoft.com/en-us/visualstudio/modeling/… Hope this resolved your issue. Commented Dec 29, 2018 at 12:11

1 Answer 1

3

I've written a clean library Razor.Templating.Core that works with .NET Core 3.0, 3.1 on both web and console app. It's available as NuGet package. After installing, you can call like

var htmlString = await RazorTemplateEngine
                      .RenderAsync("/Views/ExampleView.cshtml", model, viewData);

Note: Above snippet won't work straight away. Please refer the below working guidance on how to apply it.

Complete Working Guidance: https://medium.com/@soundaranbu/render-razor-view-cshtml-to-string-in-net-core-7d125f32c79

Sample Projects: https://github.com/soundaranbu/RazorTemplating/tree/master/examples

Sign up to request clarification or add additional context in comments.

3 Comments

your library doesn't work for me. I vahe folder LetterTemplates in my project with views and when i call var htmlString = await RazorTemplateEngine.RenderAsync("/LetterTemplates /Previews/_Preview.cshtml", model, viewData); I receive this: Unable to find view '/LetterTemplates /Previews/_Preview.cshtml'. The following locations were searched: /LetterTemplates /Previews/_Preview.cshtml
@alex-white I don't know how you applied the library. Here is the complete guidance on how to use it medium.com/@soundaranbu/… Also take a look at the example projects here github.com/soundaranbu/RazorTemplating/tree/master/examples Let me know if you still face the issue.
To fix that error, follow this page: github.com/soundaranbu/Razor.Templating.Core read section "How to render razor views from absolute path"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.