我正在使用Razor引擎https://github.com/Antaris/RazorEngine来解析我的电子邮件模板的正文。是否可以定义布局并包含其他.cshtml文件?例如一个公共页眉和一个页脚。
答案 0 :(得分:53)
在这两个帖子的帮助下,我得到了常见的模板和布局:
RazorEngine string layouts and sections?
这是我的解决方案:
解决方案1: 布局
通过设置_Layout
来使用@{
_Layout = "Layout.cshtml";
ViewBag.Title = Model.Title;
}
<强>页脚强>
@section Footer
{
@RenderPart("Footer.cshtml")
}
<强> Layout.cshtml 强>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html>
<head>
</head>
<body>
<div id="content">
@RenderBody()
</div>
@if (IsSectionDefined("Footer"))
{
<div id="footer">
@RenderSection("Footer")
</div>
}
</body>
</html>
<强> TemplateBaseExtensions 强>
使用RenderPart方法扩展TemplateBase
public abstract class TemplateBaseExtensions<T> : TemplateBase<T>
{
public string RenderPart(string templateName, object model = null)
{
string path = Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase, "Templates", templateName);
return Razor.Parse(File.ReadAllText(path), model);
}
}
Razor配置
将BaseTemplateType设置为TemplateBaseExtensions类
TemplateServiceConfiguration templateConfig = new TemplateServiceConfiguration
{
BaseTemplateType = typeof(TemplateBaseExtensions<>)
};
Razor.SetTemplateService(new TemplateService(templateConfig));
修改解决方案2:
如果您使用的是TemplateResolver。不需要RenderPart使用@Include而不是
<强>页脚强>
@section Footer
{
@Include("Footer.cshtml")
}
<强>解析器强>
public class TemplateResolver : ITemplateResolver
{
public string Resolve(string name)
{
if (name == null)
{
throw new ArgumentNullException("name");
}
string path = Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase, "Templates", name);
return File.ReadAllText(path, System.Text.Encoding.Default);
}
}
<强>配置强>
TemplateServiceConfiguration templateConfig = new TemplateServiceConfiguration
{
Resolver = new TemplateResolver()
};
Razor.SetTemplateService(new TemplateService(templateConfig));
The Muffin Man的更新 指定模板并呈现字符串
var templateResolver = Razor.Resolve("Registration.cshtml");
return templateResolver.Run(new ExecuteContext());
此外,我和此链接中的其他人https://github.com/Antaris/RazorEngine/issues/61在使用_Layout
而Layout
工作时遇到了问题。
'_ Layout'是旧语法。它在未来版本中更新为“布局”。
答案 1 :(得分:0)
你可以用Razor轻松做很多事情;然而,这个特定的项目似乎抽象了你可以做的很多Razor引擎(这既好又坏)。在你的情况下,听起来你会更好地实现自己的Razor解决方案(它实际上并没有那么糟糕)然后你可以让你的模板抛出异常或很容易地引入其他内容。
例如;滚动你自己的解决方案允许你为你的剃刀模板创建一个基类,它可以通过调用其他模板来提供引入“部分视图”的能力。此外,如果某些属性为null,则可以进行模型检查并抛出异常。
答案 2 :(得分:0)
使用RazorEngine实现布局的最简单方法是替换布局的@RenderBody()中返回的模板:
var finalHtml = layout.Replace(@"@RenderBody()", templateHtml);
<强> E.g:强>
您的 _Layout.cshtml 与典型的@RenderBody()
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html>
<head>
</head>
<body>
<div>
@RenderBody()
</div>
</body>
</html>
您的RazorEngine模板 MyTemplate.cshtml
@using RazorEngine.Templating
@inherits TemplateBase<myviewmodel>
<h1>Hello People</h1>
<p>@Model</p>
无论你在哪里调用RazorEngine模板:
var TemplateFolderPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "EmailTemplates");
var template = File.ReadAllText(Path.Combine(TemplateFolderPath,"MyTemplate.cshtml"));
var layout = File.ReadAllText(Path.Combine(TemplateFolderPath, "_Layout.cshtml"));
var templateService = new TemplateService();
var templateHtml = templateService.Parse(template, myModel, null, null);
var finalHtml = layout.Replace(@"@RenderBody()", templateHtml);
答案 3 :(得分:0)
实现邮件功能的完全自定义解决方案。
添加 RazorEngine
的nuget数据包创建_Layout模板(.cshtml):
<html>
<body style="margin: 0; padding: 0;">
<div style="width:100%; display:block; float:left; height:100%;">
<table cellpadding="0" cellspacing="0" border="0" align="center" width="100%">
<tr>
<td width="37" style="background-color: #ffffff;"></td>
<td width="200" style="background-color: #ffffff">
<a href="@Url("")">Send Mail Logo</a>
</td>
<td style="background-color: #ffffff;">
</td>
<td width="126" style="background-color: #ffffff;">
<img src="@Url("Images/mail/social-media.png")" alt="" width="126" height="73" border="0" usemap="#Map" />
</td>
</tr>
</table>
<table cellpadding="0" cellspacing="0" border="0" align="center" width="100%">
<tr height="7">
<td style="background-color: #ffffff;" colspan="3"></td>
</tr>
<tr height="54">
<td colspan="3"></td>
</tr>
<tr>
<td width="37"> </td>
<td style="font-family: Myriad, 'Helvetica Neue',Arial,Helvetica,sans-serif; font-size: 11pt; color: #6b6c6f; line-height: 24px;">
{{BODY}}
</td>
<td width="37"> </td>
</tr>
<tr height="55">
<td style="line-height: 0;" colspan="3"> </td>
</tr>
<tr height="11">
<td background="@Url("/Images/mail/dotted-line.png")" colspan="3" style="line-height: 0;"> </td>
</tr>
</table>
</div>
<map name="Map" id="Map">
<area shape="rect" coords="28,29,51,51" href="#" alt="Twitter" />
<area shape="rect" coords="56,28,78,52" href="#" alt="Google+" />
<area shape="rect" coords="84,28,104,51" href="#" alt="LinkedIn" />
</map>
</body>
</html>
创建ConfirmEmail模板(.cshtml):
@using yourProjectnamespace.LanguageResources.Mail
@model ConfirmEmail
@MailTemplateResource.YouHaveLoggedIn
<a href="@Url(string.Format("/User/Confirmemail?EmailId={0}", Model.UserId))">@MailTemplateResource.ClickHere</a>
创建CustomTemplateBase类:
public class CustomTemplateBase<T> : TemplateBase<T>
{
public string Url(string url)
{
return MailConfiguration.BaseUrl + url.TrimStart('/');
}
}
创建EmbeddedTemplateManager类:
内部类EmbeddedTemplateManager:ITemplateManager { 私有只读字符串ns;
public EmbeddedTemplateManager(string @namespace)
{
ns = @namespace;
}
public ITemplateSource Resolve(ITemplateKey key)
{
var resourceName = $"{ns}.{key.Name}.cshtml";
string content;
using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName))
using (var streamReader = new StreamReader(stream))
{
content = streamReader.ReadToEnd();
}
return new LoadedTemplateSource(content);
}
public ITemplateKey GetKey(string name, ResolveType resolveType, ITemplateKey context)
{
return new NameOnlyTemplateKey(name, resolveType, context);
}
public void AddDynamic(ITemplateKey key, ITemplateSource source)
{
throw new NotImplementedException("");
}
}
创建邮件类别:
public class Mail
{
private static readonly IRazorEngineService RazorEngine;
static Mail()
{
var config = new TemplateServiceConfiguration
{
BaseTemplateType = typeof(CustomTemplateBase<>),
TemplateManager = new EmbeddedTemplateManager(typeof(Mail).Namespace + ".Templates"),
Namespaces = { "Add CurrentProjectName", "Add CurrentProjectName .Models" },
CachingProvider = new DefaultCachingProvider()
};
RazorEngine = RazorEngineService.Create(config);
}
public Mail(string templateName)
{
TemplateName = templateName;
ViewBag = new DynamicViewBag();
}
public string TemplateName { get; set; }
public object Model { get; set; }
public DynamicViewBag ViewBag { get; set; }
public string GenerateBody()
{
var layout = RazorEngine.RunCompile("_Layout", model: null);
var body = RazorEngine.RunCompile(TemplateName, Model.GetType(), Model);
return layout.Replace("{{BODY}}", body);
}
public MailMessage Send(Guid key, string to, string subject, string cc = null)
{
var email = new MailMessage()
{
From = MailConfiguration.From,
Body = GenerateBody(),
IsBodyHtml = true,
Subject = subject,
BodyEncoding = Encoding.UTF8
};
email.Headers.Add("X-MC-Metadata", "{ \"key\": \"" + key.ToString("N") + "\" }");
foreach (var sendTo in to.Split(' ', ',', ';'))
{
email.To.Add(sendTo);
}
if (cc != null)
{
foreach (var sendCC in cc.Split(' ', ',', ';'))
{
email.CC.Add(sendCC);
}
}
var smtp = new MailClient().SmtpClient;
smtp.EnableSsl = true;
smtp.Send(email);
return email;
}
}
public class Mail<TModel> : Mail where TModel : class
{
public Mail(string templateName, TModel mailModel) : base(templateName)
{
Model = mailModel;
}
}
创建MailClient类:
public class MailClient
{
public MailClient()
{
SmtpClient = new SmtpClient(MailConfiguration.Host)
{
Port = MailConfiguration.Port,
Credentials = new NetworkCredential
{
UserName = MailConfiguration.UserName,
Password = MailConfiguration.Password
}
};
}
public SmtpClient SmtpClient { get; }
}
创建MailConfiguration类:
public class MailConfiguration
{
private static string GetAppSetting(string key)
{
var element = ConfigurationManager.AppSettings["Mail:" + key];
return element ?? string.Empty;
}
public static string BaseUrl => GetAppSetting("BaseUrl");
public static string Host => GetAppSetting("Host");
public static int Port => Int32.Parse(GetAppSetting("Port"));
public static string UserName => GetAppSetting("Username");
public static string Password => GetAppSetting("Password");
public static MailAddress From => new MailAddress(GetAppSetting("From"));
}
MailSender类:
在MailSerder类中实现您的方法,并在存储库或Controller中调用MailSerder方法。
Create public class MailSender : IMailSender
{
public MailSender()
{
}
public void SendConfirmEmail(string emailId, Guid userId)
{
var confirmEmail = new ConfirmEmail
{
UserId = userId
};
ConfirmEmail(emailId, MailResource.YourRegistration, confirmEmail);
}
private void ConfirmEmail(string recipient,string subject,ConfirmEmail model)
{
var key = Guid.NewGuid();
var mail = new Mail<ConfirmEmail>("ConfirmEmail", model);
mail.ViewBag.AddValue("Recipient", recipient);
var sentMail = mail.Send(key, recipient, subject);
}
}