Я использую механизм бритвы следующим образом:
public class EmailService : IService
{
private readonly ITemplateService templateService;
public EmailService(ITemplateService templateService)
{
if (templateService == null)
{
throw new ArgumentNullException("templateService");
}
this.templateService = templateService;
}
public string GetEmailTemplate(string templateName)
{
if (templateName == null)
{
throw new ArgumentNullException("templateName");
}
Assembly assembly = Assembly.GetAssembly(typeof(EmailTemplate));
Stream stream = assembly.GetManifestResourceStream(typeof(EmailTemplate), "{0}.cshtml".FormatWith(templateName));
string template = stream.ReadFully();
return template;
}
public string GetEmailBody(string templateName, object model = null)
{
if (templateName == null)
{
throw new ArgumentNullException("templateName");
}
string template = GetEmailTemplate(templateName);
string emailBody = templateService.Parse(template, model, null, null);
return emailBody;
}
}
Используется служба шаблонов, которую я использую, хотя это просто реализация по умолчанию:
internal ITemplateService InstanceDefaultTemplateService()
{
ITemplateServiceConfiguration configuration = new TemplateServiceConfiguration();
ITemplateService service = new TemplateService(configuration);
return service;
}
Так как в этом случае, в частности, я буду создавать электронные письма из этих шаблонов. Я хочу иметь возможность использовать @section
для темы электронной почты и разных разделов тела электронной почты, используя макет, где я указываю стили, которые являются общими для всей структуры электронной почты (которая будет выглядеть как одна из MailChimp, вероятно).
Вопрос тогда двоякий:
- Как я могу указать макеты в
RazorEngine
? - Как я могу указать эти макеты из строк (или потока)? поскольку, как вы можете видеть, я использую встроенные ресурсы для хранения шаблонов электронной почты бритвы.
Update
Возможно, я не был ясен, но я имею в виду библиотеку RazorEngine.