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.

Raju Paladiya
  • 778
  • 2
  • 12
  • 35
  • Possible duplicate of [Return View as String in .NET Core](https://stackoverflow.com/questions/40912375/return-view-as-string-in-net-core) – Peter B Dec 28 '18 at 13:35
  • it's not wokting code and I think that code for web not console app – Raju Paladiya Dec 28 '18 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. https://learn.microsoft.com/en-us/visualstudio/modeling/run-time-text-generation-with-t4-text-templates?view=vs-2017 Hope this resolved your issue. – rakesh girase Dec 29 '18 at 12:11

1 Answers1

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

Soundar Anbu
  • 129
  • 5
  • 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 Jul 01 '20 at 21:41
  • @alex-white I don't know how you applied the library. Here is the complete guidance on how to use it https://medium.com/@soundaranbu/render-razor-view-cshtml-to-string-in-net-core-7d125f32c79 Also take a look at the example projects here https://github.com/soundaranbu/RazorTemplating/tree/master/examples Let me know if you still face the issue. – Soundar Anbu Jul 03 '20 at 04:44
  • To fix that error, follow this page: https://github.com/soundaranbu/Razor.Templating.Core read section "How to render razor views from absolute path" – Sergey Apr 14 '23 at 08:08