46

我正在使用 Razor 引擎https://github.com/Antaris/RazorEngine来解析我的电子邮件模板的正文。是否可以定义布局并包含其他 .cshtml 文件?例如常见的页眉和页脚。

4

4 回答 4

55

在这两个帖子的帮助下,我得到了通用模板和布局:

RazorEngine 字符串布局和部分?

http://blogs.msdn.com/b/hongyes/archive/2012/03/12/using-razor-template-engine-in-web-api-self-host-application.aspx

这是我的解决方案:

解决方案 1: 布局

通过设置 _Layout 使用

@{
    _Layout = "Layout.cshtml";
    ViewBag.Title = Model.Title;
 }

页脚

@section Footer 
{
   @RenderPart("Footer.cshtml")
}

布局.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>

模板库扩展

使用 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);
    }
}

剃刀配置

将 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' 是旧语法。在未来的版本中已更新为“布局”。

于 2012-08-03T15:39:09.057 回答
1

你可以用 Razor 轻松地做很多事情;然而,那个特定的项目似乎抽象出了很多你可以做的 Razor 引擎的东西(这有好有坏)。在您的情况下,听起来您最好实现自己的 Razor 解决方案(实际上并没有那么糟糕),然后您可以让您的模板抛出异常或很容易地拉入其他内容。

例如; 滚动您自己的解决方案允许您为剃刀模板创建一个基类,该基类可以公开通过调用其他模板来拉入“部分视图”的能力。此外,如果某些属性为空,您可以进行模型检查并抛出异常。

于 2012-07-10T13:59:19.850 回答
1

使用 RazorEngine 实现布局的最简单方法是替换模板在布局的 @RenderBody() 中返回的内容:

 var finalHtml = layout.Replace(@"@RenderBody()", templateHtml);

例如:

您的_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);
于 2016-10-02T18:24:55.743 回答
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;">
                    &nbsp;

                </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">&nbsp;</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">&nbsp;</td>
            </tr>

            <tr height="55">
                <td style="line-height: 0;" colspan="3">&nbsp;</td>
            </tr>
            <tr height="11">
                <td background="@Url("/Images/mail/dotted-line.png")" colspan="3" style="line-height: 0;">&nbsp;</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 { private readonly string 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"));
    }

邮件发件人类:

在 MailSerder 类中实现您的方法并在您的存储库或控制器中调用 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);          
        }
    }
于 2019-01-22T08:38:09.353 回答