RazorFormat API changes

I need to generate some HTML emails and was following this

But it seems the API has changed, as RazorFormat (in 5.0.2) no longer has a GetViewPage or CreateAndRenderToHtml.

Digging around GitHub I don’t see where it moved

Is there a different way?

Those APIs still exist on .NET v4.5 of ServiceStack.Razor RazorFormat.

Razor on .NET Core is a completely new and different implementation where we no longer have control over the implementation of Razor and instead use MVC’s Razor APIs. We have some APIs .NET Core’s RazorFormat like GetPageFromPathInfo(), FindView() and RenderView() which will let you render to a Stream but for any other functionality you’d also need to use MVC’s APIs directly but I’d recommend taking a look at http://templates.servicestack.net which is a highly versatile templating language that’s well suited for Emails without the issues of using Razor.

1 Like

This will do nicely, thank you.

I’m a little confused about the usage of VirtualFiles. Essentially I just want to throw all my email templates in an EmailTemplates folder (with text and html versions)

UserRegistrationEmailHtml.html
UserRegistrationEmailPlainText.md

And use my Dto name to generate each version

var templateNameHtml = typeof(dto).Name + “Html.html”
var templateNamePlainText = typeof(dto).Name + “PlainText.md”

I see this blurb about Resolution

There's no forced special centralized folders like /Views or /Views/Shared required to store layouts or share partials or artificial "Areas" concept to isolate website sections. Different websites or sections are intuitively grouped into different folders and Templates automatically resolves the closest layout it finds for each page. Cascading resolution also applies to including files or partial pages where you can use just its name to resolve the closest one, or an absolute path from the WebRootPath to include a specific partial or file from a different folder.

But where do I search for or register all my templates? Upon startup Config, would I want to scan my folder for all files of type html and md in a specific folder and write them similarly to the Email Example?

The VirtualFiles in Templates uses ServiceStack’s VirtualFileSystem. If you use the TemplatePagesFeature (which inherits TemplateContext) it’s already configured to use the same Virtual File Sources as your ServiceStack AppHost. So you can call context.GetPage() and it will look in your .NET Core App’s ContentRootPath.

If you’re instead using a new TemplateContext instance like in our Email Templates example you can choose to either write the files to the In Memory Virtual FileSystem or configure the VirtualFiles to use a specified directory, e.g:

context.VirtualFiles = new FileSystemVirtualFiles(env.ContentRootPath);
1 Like