My console app needs to send HTML emails. I'd like to write the emails in HTML format in a Razor view and have the engine generate the email body content.
This means no controllers or requests. How could I go about this?
My console app needs to send HTML emails. I'd like to write the emails in HTML format in a Razor view and have the engine generate the email body content.
This means no controllers or requests. How could I go about this?
There is an open source project which allows to use Razor as a general templating engine: it's called RazorEngine (the code in on GitHub)
A sample for the project's page:
string template = "Hello @Model.Name! Welcome to Razor!";
string result = Razor.Parse(template, new { Name = "World" });
I have a demo project on GitHub that demonstrates how to use Razor views to render content outside of the context of a running ASP.Net application. It provides full support for layouts, _ViewStart files, partials, HtmlHelper, Urlhelper etc.
Note that it uses the RazorGenerator Visual Studio tool to precompile Razor templates.
See my introductory blog post for further background.